当前浏览:首页 / WordPress函数 / shortcode_atts()

shortcode_atts( array $pairs, array $atts, string $shortcode = '' )

将用户属性与已知属性相结合,并在需要时填写默认值

shortcode 短码


描述

这些对应被视为调用者支持的所有属性,并作为列表给出。返回的属性将仅包含$pairs列表中的属性。

如果$atts列表具有不受支持的属性,则这些属性将被忽略并从最终返回的列表中删除。


参数

$pairs

(array)(必填) 支持的属性及其默认值的完整列表。

$atts

(array)(必填) 短代码标签中的用户定义属性。

$shortcode

(string)(可选) 短代码的名称,为上下文提供以启用过滤

默认值: ''


返回

(array) 组合并过滤的属性列表。



源码

查看源码 官方文档


更新日志

版本描述
2.5.0开始引入

使用示例

  • 示例1

    请注意,属性名称必须全部小写,这可能非常有用…

    如果您的短代码如下所示:
    [shortcode attOne="foo" attTwo="bar"]
    传递给回调函数的数组将如下所示
    Array( attone => "foo", atttwo => "bar" )

    所以,我想你可以在短代码中使用camelCase,只要记住在shortcode_atts中那些camelCase都是小写的…

  • 示例2

    实例

    /**
     * Callback to register a bartag shortcode.
     *
     * @param array $atts Shortcode attributes.
     * @return string Shortcode output.
    function wpdocs_bartag_func( $atts ) {
    	$atts = shortcode_atts(
    		array(
    			'foo' => 'no foo',
    			'bar' => 'default bar',
    		), $atts, 'bartag' );
    
    	return 'bartag: ' . esc_html( $atts['foo'] ) . ' ' . esc_html( $atts['bar'] );
    }
    add_shortcode( 'bartag', 'wpdocs_bartag_func' );
    

    [bartag foo=”koala” bar=”bears”] 输出以下内容:
    bartag: koala bears

    [bartag foo=”koala”] 输出以下内容:
    bartag: koala default bar

    这将创建一个支持两个属性的“[bartag]”短代码:[bartag foo="something" bar="something else"]。这两个属性都是可选的,如果未提供,则将采用默认选项。

  • 示例3

    关于Aaron的笔记的澄清

    $pairs上的键的大小写必须与$atts中的键匹配,否则该值将被丢弃。

    参见以下示例:

    print_r( shortcode_atts( array(
    	'CAPITAL' => '1',
    	'fooBar' => '2',
    ), array(
    	'CAPITAL' => '3',
    	'fooBar' => '4',
    ) ) );
    // Output: Array ( [CAPITAL] => 3 [fooBar] => 4 )
    
    print_r( shortcode_atts( array(
    	'CAPITAL' => '1',
    	'fooBar' => '2',
    ), array(
    	'capital' => '3',
    	'fooBar' => '4',
    ) ) );
    // Output: Array ( [CAPITAL] => 1 [fooBar] => 4 )
    
    print_r( shortcode_atts( array(
    	'CAPITAL' => '1',
    	'fooBar' => '2',
    ), array(
    	'capital' => '3',
    	'foobar' => '4',
    ) ) );
    // Output: Array ( [CAPITAL] => 1 [fooBar] => 2 )
    
    print_r( shortcode_atts( array(
    	'capital' => '1',
    	'fooBar' => '2',
    ), array(
    	'capital' => '3',
    	'foobar' => '4',
    ) ) );
    // Output: Array ( [capital] => 3 [fooBar] => 2 )
    
  • 示例4

    为了确保此函数正常工作并允许不区分大小写的属性,我在一行程序中使用array_change_key_case,如下所示

    /**
     * My Custom Shortcode
     * 
     * @param array $atts Optional. An associative array of key/value attributes. Default is empty array.
     * @param string $content Optional. The content between the opening and closing shortcode tags. Default is an empty string.
     * @param string $tag Optional. The name of the shortcode, provided for context to enable filtering. Default is an empty string.
     * 
     * @return string shortcode output
     */
    function tw_custom_shortcode($atts = array(), $content = '', $tag = '')
    {
        $atts = shortcode_atts(array(), array_change_key_case((array)$atts, CASE_LOWER), $tag);
    
        //Do stuff, probably sanitize inputs
    
        $output = '';
        return $output;
    }
    add_shortcode('tw_custom', 'tw_custom_shortcode');
  • 示例5

    我不得不四处寻找,以找到所有的碎片,我希望其他人觉得这有帮助。

    因此,在前面一个例子的基础上:

    /**
     * How to use shortcode callback parms with shortcode_atts to make
     * callback code flexible and re-useable.
     *
     * This allows filters to be applied to a callback depending on which
     * shortcode called it.
     *
     * @param array  $atts    Shortcode attributes.
     * @param string $content Shortcode content.
     * @param string $tag     The shortcode which invoked the callback
     * @return string Shortcode output.
     */
    function my_callback_func( $atts, $content, $tag ) {
        $atts = shortcode_atts(
            array(
                'foo' => '',
                'bar' => '',
            ), $atts, $tag );
    
     //do something here(trivial example)
    	if ( !empty($zoo) ) {
    		return $zoo;  //where did $zoo come from? see below
    	}
    	else return $foo;
        
    }
    
    add_shortcode( 'myshortcode1', 'my_callback_func' );
    add_shortcode( 'myshortcode2', 'my_callback_func' );
    
    /* now make a filter that's applied ONLY when my_callback_func is invoked by myshortcode1 -
    this example adds param 'zoo' to myshortcode1  */
    
    add_filter ('shortcode_atts_myshortcode1', 'add_zoo', 10, 3);
    function add_zoo ($out, $pairs, $atts ) {
    	$out['zoo'] = '';
    	return $out;
    }
    // Now we can use [myshortcode1 foo='myfoo' zoo='myzoo']  output: myzoo
    // and [myshortcode2 foo='myfoo' zoo='myzoo']  output: myfoo