Wordpress短代码传递值数组

mcn*_*a86 10 php wordpress shortcode

我正在创建一些WordPress短代码,旨在提供页面上的内部导航(一页有很多内容部分和它自己的菜单).

这就是我所拥有的:

//menu
function internal_menu($atts) {
  extract(shortcode_atts(array(
   'href1' => '#jl1',
   'href2' => '#jl2',
   'href3' => '#jl3',
   'href4' => '#jl4',
  ), $atts));
  return '<div id="internalPageMenu">
    <ul>
        <li><a href="' . $href1 . '"><i class="fa fa-bars"></i>link 1</a></li>
        <li><a href="' . $href2 . '">link 2</a></li>
        <li><a href="' . $href3 . '">link 3</a></li>
        <li><a href="' . $href4 . '">link 4</a></li>
    </ul>
    </div>';
}
add_shortcode('internal-menu', 'internal_menu');

//menu target
function internal_menu_target($atts) {
  extract(shortcode_atts(array(
   'id' => 'jl1',
   'text' => '',
   ), $atts));
   return '<h3 id="' . $id . '">' . $text . '</h3>';
}
add_shortcode('internal-menu-target', 'internal_menu_target');
Run Code Online (Sandbox Code Playgroud)

并在我的Wordpress管理面板中使用它:

[internal-menu]
[internal-menu-target id="jl1"]
Some content
[internal-menu-target id="jl2"]
...etc...
Run Code Online (Sandbox Code Playgroud)

如何使菜单动态(不限于它可以拥有的项目数量)?例如,短代码将是:

[internal-menu targets="jl1, jl2, jl3, jl4, jl5, ...etc..."]
Run Code Online (Sandbox Code Playgroud)

Pie*_*sen 16

foreach这是你的答案.在我看来,这将是最简单,最干净的.在我给你一个代码示例之前,让我们分析你的代码并查看你的所有缺陷以及我们将如何纠正它们

破绽

  • 永远不要使用extract().exctract()动态创建变量是有问题的.你无法正常调试extract()(如果你能做到的话),所以当它失败时你真的会为你做好工作,不必要的.由于这些原因,它完全从核心和手抄本中删除.见trac ticket 22400.你应该有一个邪恶的名单,query_posts并且extract()在前两个位置,这两个是多么糟糕.

  • 您没有清理和验证输入数据,这可能导致黑客将jquery注入您的代码以破解您的网站.永远不要相信来自用户端和URL的任何数据,它可能会被感染.

  • 如您所知,从您的代码中获取,短代码不能除数组值外,值必须是字符串.在您的情况下,我们需要从字符串值创建一个数组.同样,因为你不能相信用户在逗号之前或之后不使用空格,所以明智地建议删除所有空格(如果有的话),以便你的explode函数正确地创建你的数组

  • 使用这种新方法,您需要确保字符串中的值的顺序正确,并且字符串是正确的长度.如果没有,您将获得意外的输出

让我们解决第一个短代码 :( 请注意:下面的所有代码都是未经测试的.它可能有错误或有语法错误)

internal-menu

//menu
function internal_menu( $atts ) 
{
    $attributes = shortcode_atts(
        array(
           'href' => '',
         ), 
        $atts
    );

    $output = '',
    // Check if href has a value before we continue to eliminate bugs
    if ( !$attribute['href'] )
        return $output;
    // Create our array of values
    // First, sanitize the data and remove white spaces
    $no_whitespaces = preg_replace( '/\s*,\s*/', ',', filter_var( $attributes['href'], FILTER_SANITIZE_STRING ) ); 
    $href_array = explode( ',', $no_whitespaces );

    $output .= '<div id="internalPageMenu">';
        $output .= '<ul>';

            foreach ( $href_array as $k => $v ) { 
                // From your code, link 1 is different, so I kept it as is
                if ( $k == 0 ) {
                    $output .= '<li><a href="#' . $v . '"><i class="fa fa-bars"></i>link 1</a></li>';
                } else { 
                    $output .= '<li><a href="#' . $v . '">link ' . ($k + 1 ) . '</a></li>';
                }
            }

        $output .= '</ul>';
    $output .= '</div>';

    return $output;
}
add_shortcode( 'internal-menu', 'internal_menu' );
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用以下短代码

[internal-menu href='jl1, jl2, jl3, jl4']
Run Code Online (Sandbox Code Playgroud)

internal-menu-target

//menu target
function internal_menu_target($atts) 
{
    $attributes = shortcode_atts(
        array(
           'id' => '',
           'text' => '',
         ), 
        $atts
    );

    $output = '',
    // Check if href has a value before we continue to eliminate bugs
    if ( !$attribute['id'] || !$attribute['text'] )
        return $output;

    // Create our array of values
    // First, sanitize the data and remove white spaces
    $no_whitespaces_ids = preg_replace( '/\s*,\s*/', ',', filter_var( $attributes['id'], FILTER_SANITIZE_STRING ) ); 
    $ids_array = explode( ',', $no_whitespaces_ids );

    $no_whitespaces_text = preg_replace( '/\s*,\s*/', ',', filter_var( $attributes['text'], FILTER_SANITIZE_STRING ) ); 
    $text_array = explode( ',', $no_whitespaces_text );

    // We need to make sure that our two arrays are exactly the same lenght before we continue
    if ( count( $ids_array ) != count( $text_array ) )
        return $output;

    // We now need to combine the two arrays, ids will be keys and text will be value in our new arrays
    $combined_array = array_combine( $ids_array, $text_array );
    foreach ( $combined_array as $k => $v )
        $output .= '<h3 id="' . $k . '">' . $v . '</h3>';

    return $output;
}
add_shortcode('internal-menu-target', 'internal_menu_target');
Run Code Online (Sandbox Code Playgroud)

您可以使用以下短代码:

[internal-menu-target id='1,2,3,4' text='text 1, text 2, text 3, text 4']
Run Code Online (Sandbox Code Playgroud)