Wordpress 简码传递值数组 [英] Wordpress shortcodes pass array of values

查看:37
本文介绍了Wordpress 简码传递值数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这就是我所拥有的:

//菜单函数 internal_menu($atts) {提取(shortcode_atts(数组('href1' =>'#jl1','href2' =>'#jl2','href3' =>'#jl3','href4' =>'#jl4',), $atts));return '

<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>

';}add_shortcode('internal-menu', 'internal_menu');//菜单目标函数 internal_menu_target($atts) {提取(shortcode_atts(数组('id' =>'jl1','文本' =>'',), $atts));返回 '​​<h3 id="' . $id . '">'.$文本.'</h3>';}add_shortcode('internal-menu-target', 'internal_menu_target');

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

[内部菜单][内部菜单目标 id="jl1"]部分内容[内部菜单目标 id="jl2"]...等等...

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

[internal-menu targets="jl1, jl2, jl3, jl4, jl5, ...etc..."]

解决方案

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

缺陷

  • 永远不要使用 extract().exctract() 动态创建变量,这是有问题的.您无法正确调试 extract()(如果您甚至可以),因此当它失败时,您确实会不必要地为您完成工作.由于这些原因,它从核心和手抄本中完全删除.请参阅 trac 票 22400.您应该有一个邪恶列表,其中 query_postsextract() 位于前两个位置,这两个位置有多糟糕.

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

  • 正如您已经知道的,从您的代码中获取,短代码不能除了数组值,值必须是字符串.在您的情况下,我们需要从字符串值创建一个数组.同样,由于您不能相信用户不会在逗号前后使用空格,因此明智的做法是删除所有空格(如果有),以便您的 explode 函数正确创建您的数组

  • 使用这种新方法,您需要确保字符串中的值的顺序正确且字符串的长度正确.如果没有,你会得到意想不到的输出

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

内部菜单

//菜单函数 internal_menu( $atts ){$attributes = shortcode_atts(大批('href' =>'',),美元);$输出 = '',//在我们继续消除bug之前检查href是否有值如果 ( !$attribute['href'] )返回 $output;//创建我们的值数组//首先,清理数据并删除空格$no_whitespaces = preg_replace('/\s*,\s*/', ',', filter_var( $attributes['href'], FILTER_SANITIZE_STRING));$href_array =explode(',', $no_whitespaces);$output .= '

';$output .='

然后您可以使用以下简码

[internal-menu href='jl1, jl2, jl3, jl4']

内部菜单目标

//菜单目标函数 internal_menu_target($atts){$attributes = shortcode_atts(大批('id' =>'','文本' =>'',),美元);$输出 = '',//在我们继续消除bug之前检查href是否有值if ( !$attribute['id'] || !$attribute['text'] )返回 $output;//创建我们的值数组//首先,清理数据并删除空格$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);//在继续之前,我们需要确保我们的两个数组的长度完全相同if ( count( $ids_array ) != count( $text_array ) )返回 $output;//我们现在需要组合两个数组,id 将是键,文本将是我们新数组中的值$combined_array = array_combine( $ids_array, $text_array );foreach ( $combined_array as $k => $v )$output .= '<h3 id="' . $k . '">'.$v .'</h3>';返回 $output;}add_shortcode('internal-menu-target', 'internal_menu_target');

您可以按如下方式使用此简码:

[internal-menu-target id='1,2,3,4' text='text 1, text 2, text 3, text 4']

I am creating some WordPress short codes aimed at providing internal navigation on a page (one page with a lot of content sections and it own menu).

This is what I have:

//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');

And using this in my Wordpress admin panel:

[internal-menu]
[internal-menu-target id="jl1"]
Some content
[internal-menu-target id="jl2"]
...etc...

How do I make the menu dynamic (not restricted to the number of items it can have)? For example the short code would be:

[internal-menu targets="jl1, jl2, jl3, jl4, jl5, ...etc..."]

解决方案

foreach would be your answer here. It will be the easiest and cleanest in my opinion. Before I give you a code example, lets analyze your code and look at all your flaws and how we will correct them

FLAWS

Lets tackle the first shortcode: (PLEASE NOTE: All the code below is untested. It might be buggy or have syntax errors)

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' );

You can then use the shortcode as follow

[internal-menu href='jl1, jl2, jl3, jl4']

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');

You can use this shortcode as follow:

[internal-menu-target id='1,2,3,4' text='text 1, text 2, text 3, text 4']

这篇关于Wordpress 简码传递值数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆