shortcode_atts

函数
shortcode_atts ( $pairs, $atts, $shortcode = '' )
参数
  • (array) $pairs Entire list of supported attributes and their defaults.
    Required:
  • (array) $atts User defined attributes in shortcode tag.
    Required:
  • (string) $shortcode Optional. The name of the shortcode, provided for context to enable filtering
    Required:
    Default: (empty)
返回值
  • (array) Combined and filtered attribute list.
定义位置
相关方法
shortcode_parse_attsshortcode_existsshortcode_unautopget_shortcode_atts_regexdo_shortcode_tag
引入
2.5.0
弃用
-

shortcode_atts: 这是一个WordPress的函数,用于解析和合并一个短码的属性和默认属性。它通常用于为短码属性设置默认值,并以用户定义的值来覆盖它们: 这个函数需要两个参数:默认属性和用户定义的属性。

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

对应该被认为是调用者所支持的所有属性,并以列表形式给出。返回的属性将只包含 $pairs 列表中的属性。

如果$atts列表中有不支持的属性,那么它们将被忽略并从最终返回的列表中删除。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function shortcode_atts( $pairs, $atts, $shortcode = '' ) {
$atts = (array) $atts;
$out = array();
foreach ( $pairs as $name => $default ) {
if ( array_key_exists( $name, $atts ) ) {
$out[ $name ] = $atts[ $name ];
} else {
$out[ $name ] = $default;
}
}
if ( $shortcode ) {
/**
* Filters shortcode attributes.
*
* If the third parameter of the shortcode_atts() function is present then this filter is available.
* The third parameter, $shortcode, is the name of the shortcode.
*
* @since 3.6.0
* @since 4.4.0 Added the `$shortcode` parameter.
*
* @param array $out The output array of shortcode attributes.
* @param array $pairs The supported attributes and their defaults.
* @param array $atts The user defined shortcode attributes.
* @param string $shortcode The shortcode name.
*/
$out = apply_filters( "shortcode_atts_{$shortcode}", $out, $pairs, $atts, $shortcode );
}
return $out;
}
function shortcode_atts( $pairs, $atts, $shortcode = '' ) { $atts = (array) $atts; $out = array(); foreach ( $pairs as $name => $default ) { if ( array_key_exists( $name, $atts ) ) { $out[ $name ] = $atts[ $name ]; } else { $out[ $name ] = $default; } } if ( $shortcode ) { /** * Filters shortcode attributes. * * If the third parameter of the shortcode_atts() function is present then this filter is available. * The third parameter, $shortcode, is the name of the shortcode. * * @since 3.6.0 * @since 4.4.0 Added the `$shortcode` parameter. * * @param array $out The output array of shortcode attributes. * @param array $pairs The supported attributes and their defaults. * @param array $atts The user defined shortcode attributes. * @param string $shortcode The shortcode name. */ $out = apply_filters( "shortcode_atts_{$shortcode}", $out, $pairs, $atts, $shortcode ); } return $out; }
function shortcode_atts( $pairs, $atts, $shortcode = '' ) {
	$atts = (array) $atts;
	$out  = array();
	foreach ( $pairs as $name => $default ) {
		if ( array_key_exists( $name, $atts ) ) {
			$out[ $name ] = $atts[ $name ];
		} else {
			$out[ $name ] = $default;
		}
	}

	if ( $shortcode ) {
		/**
		 * Filters shortcode attributes.
		 *
		 * If the third parameter of the shortcode_atts() function is present then this filter is available.
		 * The third parameter, $shortcode, is the name of the shortcode.
		 *
		 * @since 3.6.0
		 * @since 4.4.0 Added the `$shortcode` parameter.
		 *
		 * @param array  $out       The output array of shortcode attributes.
		 * @param array  $pairs     The supported attributes and their defaults.
		 * @param array  $atts      The user defined shortcode attributes.
		 * @param string $shortcode The shortcode name.
		 */
		$out = apply_filters( "shortcode_atts_{$shortcode}", $out, $pairs, $atts, $shortcode );
	}

	return $out;
}

常见问题

FAQs
查看更多 >