函数文档

do_shortcode_tag()

💡 云策文档标注

概述

do_shortcode_tag() 是 do_shortcode() 的正则表达式可调用函数,用于处理短代码钩子。它解析短代码匹配数组,执行回调并应用过滤器。

关键要点

  • 函数作为 do_shortcode() 的内部回调,处理短代码的正则匹配数组 $m。
  • 支持 [[foo]] 语法转义短代码,直接返回去除外层括号的内容。
  • 解析短代码名称和属性,检查回调函数是否可调用,否则触发 _doing_it_wrong() 警告。
  • 应用 pre_do_shortcode_tag 和 do_shortcode_tag 过滤器,允许输出短路或修改。
  • 返回短代码输出,包含可能的转义括号。

代码示例

function do_shortcode_tag( $m ) {
	global $shortcode_tags;

	// Allow [[foo]] syntax for escaping a tag.
	if ( '[' === $m[1] && ']' === $m[6] ) {
		return substr( $m[0], 1, -1 );
	}

	$tag  = $m[2];
	$attr = shortcode_parse_atts( $m[3] );

	if ( ! is_callable( $shortcode_tags[ $tag ] ) ) {
		_doing_it_wrong(
			__FUNCTION__,
			sprintf( __( 'Attempting to parse a shortcode without a valid callback: %s' ), $tag ),
			'4.3.0'
		);
		return $m[0];
	}

	$return = apply_filters( 'pre_do_shortcode_tag', false, $tag, $attr, $m );
	if ( false !== $return ) {
		return $return;
	}

	$content = isset( $m[5] ) ? $m[5] : null;

	$output = $m[1] . call_user_func( $shortcode_tags[ $tag ], $attr, $content, $tag ) . $m[6];

	return apply_filters( 'do_shortcode_tag', $output, $tag, $attr, $m );
}

注意事项

  • 参数 $m 是正则匹配数组,索引对应短代码各部分,如 $m[2] 为短代码名称。
  • 使用 shortcode_parse_atts() 解析属性,确保 $attr 始终为数组。
  • 过滤器 pre_do_shortcode_tag 可短路执行,do_shortcode_tag 可修改输出。
  • 函数自 WordPress 2.5.0 引入,相关函数包括 get_shortcode_regex() 和 shortcode_parse_atts()。

📄 原文内容

Regular Expression callable for do_shortcode() for calling shortcode hook.

Description

See also

Parameters

$marrayrequired
Regular expression match array.

  • 0 string
    Entire matched shortcode text.
  • 1 string
    Optional second opening bracket for escaping shortcodes.
  • 2 string
    Shortcode name.
  • 3 string
    Shortcode arguments list.
  • 4 string
    Optional self closing slash.
  • 5 string
    Content of a shortcode when it wraps some content.
  • 6 string
    Optional second closing bracket for escaping shortcodes.

Return

string Shortcode output.

Source

function do_shortcode_tag( $m ) {
	global $shortcode_tags;

	// Allow [[foo]] syntax for escaping a tag.
	if ( '[' === $m[1] && ']' === $m[6] ) {
		return substr( $m[0], 1, -1 );
	}

	$tag  = $m[2];
	$attr = shortcode_parse_atts( $m[3] );

	if ( ! is_callable( $shortcode_tags[ $tag ] ) ) {
		_doing_it_wrong(
			__FUNCTION__,
			/* translators: %s: Shortcode tag. */
			sprintf( __( 'Attempting to parse a shortcode without a valid callback: %s' ), $tag ),
			'4.3.0'
		);
		return $m[0];
	}

	/**
	 * Filters whether to call a shortcode callback.
	 *
	 * Returning a non-false value from filter will short-circuit the
	 * shortcode generation process, returning that value instead.
	 *
	 * @since 4.7.0
	 * @since 6.5.0 The `$attr` parameter is always an array.
	 *
	 * @param false|string $output Short-circuit return value. Either false or the value to replace the shortcode with.
	 * @param string       $tag    Shortcode name.
	 * @param array        $attr   Shortcode attributes array, can be empty if the original arguments string cannot be parsed.
	 * @param array        $m      Regular expression match array.
	 */
	$return = apply_filters( 'pre_do_shortcode_tag', false, $tag, $attr, $m );
	if ( false !== $return ) {
		return $return;
	}

	$content = isset( $m[5] ) ? $m[5] : null;

	$output = $m[1] . call_user_func( $shortcode_tags[ $tag ], $attr, $content, $tag ) . $m[6];

	/**
	 * Filters the output created by a shortcode callback.
	 *
	 * @since 4.7.0
	 * @since 6.5.0 The `$attr` parameter is always an array.
	 *
	 * @param string $output Shortcode output.
	 * @param string $tag    Shortcode name.
	 * @param array  $attr   Shortcode attributes array, can be empty if the original arguments string cannot be parsed.
	 * @param array  $m      Regular expression match array.
	 */
	return apply_filters( 'do_shortcode_tag', $output, $tag, $attr, $m );
}

Hooks

apply_filters( ‘do_shortcode_tag’, string $output, string $tag, array $attr, array $m )

Filters the output created by a shortcode callback.

apply_filters( ‘pre_do_shortcode_tag’, false|string $output, string $tag, array $attr, array $m )

Filters whether to call a shortcode callback.

Changelog

Version Description
2.5.0 Introduced.