1. 首页
  2. WordPress 函数手册

shortcode_atts()

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

结合用户属性和已知属性,并在需要时填写默认值。
Combine user attributes with known attributes and fill in defaults when needed.

目录锚点:#说明#参数#源码#笔记


说明(Description)

这些对应该被认为是调用者支持的所有属性,并以列表的形式给出。返回的属性将只包含$pairs列表中的属性。如果$atts列表包含不受支持的属性,则它们将被忽略并从最终返回的列表中删除。


参数(Parameters)

参数 类型 说明
$pairs (array) 支持的属性及其默认值的完整列表。
$atts (array) 快捷代码标记中的用户定义属性。
$shortcode (string) 为上下文提供的用于启用筛选的短代码的名称

源码(Source)

/**
 * Combine user attributes with known attributes and fill in defaults when needed.
 *
 * The pairs should be considered to be all of the attributes which are
 * supported by the caller and given as a list. The returned attributes will
 * only contain the attributes in the $pairs list.
 *
 * If the $atts list has unsupported attributes, then they will be ignored and
 * removed from the final returned list.
 *
 * @since 2.5.0
 *
 * @param array  $pairs     Entire list of supported attributes and their defaults.
 * @param array  $atts      User defined attributes in shortcode tag.
 * @param string $shortcode Optional. The name of the shortcode, provided for context to enable filtering
 * @return array Combined and filtered attribute list.
 */
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;
	}
	/**
	 * Filter a shortcode's default 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
	 *
	 * @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.
	 */
	if ( $shortcode )
		$out = apply_filters( "shortcode_atts_{$shortcode}", $out, $pairs, $atts );

	return $out;
}
更新版本 源码位置 使用 被使用
2.5.0 wp-includes/shortcodes.php 1 9

笔记(Notes)

例子

shortcode_atts() 为WP2原创文章,链接:https://www.wp2.cn/functions/shortcode_atts/