2013-08-19 14:05
浏览 56


I am trying yo understand this function, as a preface to forking it to make similar functions for my own shortcodes. I understand how to define shortcodes and their functions. I also basically "get" what the original author is doing here: collecting parameters from the shortcode and assembling them into an HTML tag and returning that tag. It seems the order of the params is unimportant, but their names are.

However, when I am working with this code, it does not seem to understand which param is which. For example, the original docs say to use the shortcode like so: [button link="" color="black" size="small"]Button Text[/button]

But when I use this shortcode, I get:

<a href="Button Text" title="Array" class="button button-small button " target="_self">

Here's my PHP:

if( ! function_exists( 'make_button' ) ) {
function make_button( $text, $url, $color = 'default', $target = '_self', $size = 'small', $classes = null, $title = null ) {
    if( $target == 'lightbox' ) {
        $lightbox = ' rel="lightbox"';
        $target = null;
    } else {
        $lightbox = null;
        $target = ' target="'.$target.'"';
    if( ! $title )
        $title = $text;
    $output = '<a href="'.$url.'" title="'.$title.'" class="button button-'.$size.' '.$color.' '.$classes.'"'.$target.$lightbox.'>';
    $output .= '<span>'.$text.'</span>';
    $output .= '</a>';
    return $output;

add_shortcode( 'button', 'make_button' );

图片转代码服务由CSDN问答提供 功能建议

我正在尝试理解这个函数,作为分支它为我自己的短代码制作类似函数的序言。 我理解如何定义短代码及其功能。 我还基本上“得到”原作者在这里做的事情:从短代码中收集参数并将它们组装成HTML标记并返回该标记。 似乎params的顺序并不重要,但是它们的名字是。

然而,当我使用这段代码时,它似乎不明白哪个参数是哪个。 例如,原始文档说使用短代码如下: [button link =“”color =“black”size =“small”]按钮文字[/ button] < / code>


 &lt; a href =“Button Text”title =“ 数组“class =”按钮 - 小按钮“target =”_ self“&gt; 
&lt; span&gt;数组&lt; / span&gt; 
&lt; / a&gt; 
 <  p>这是我的PHP: 
function make_button($ text,$ url,$ color ='default',$ target  ='_ self',$ size ='small',$ classes = null,$ title = null){
 if($ target =='lightbox'){
 $ lightbox ='rel =“lightbox”'; \  n $ target = null; 
} else {
 $ lightbox = null; 
 $ target ='target =“'。$ target。'”'; 
 if(!$ title)
 $  title = $ text; 
 $ output ='&lt; a href =“'。$ url。'”title =“'。$ title。'”class =“button button  - '。$ size。'  '$的颜色。'  '。$ classes。'''。$ target。$ lightbox。'&gt;'; 
 $ output。='&lt; span&gt;'。$ text。'&lt; / span&gt;'; 
 $ output。=  '&lt; / a&gt;'; 
返回$ output; 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doucan4873 2013-08-19 14:31

    See the documentation for Shortcode API, there clearly states that three parameters are passed to the shortcode callback function:

    • $atts - an associative array of attributes, or an empty string if no attributes are given
    • $content - the enclosed content (if the shortcode is used in its enclosing form)
    • $tag - the shortcode tag, useful for shared callback functions

    So the function definition should look like:

    function make_button( $atts, $content, $tag ) {
        // use print_r to examine attributes
    解决 无用
    打赏 举报
  • dongyu5104 2013-08-19 14:22

    The shortcode is explicitly looking for $text.

    [button url="" color="black" size="small" text="Button Text"]

    Typically the variable that is set when you use the open/close shortcode is $content, per the Shortcode API. Another fix would be to change the shortcode to look for $content instead of $text.

    解决 无用
    打赏 举报

相关推荐 更多相似问题