函数文档

shortcode_unautop()

💡 云策文档标注

概述

shortcode_unautop() 函数用于防止独立的短代码被自动包裹在段落标签中。它通过正则表达式匹配并移除短代码周围的段落标签,确保短代码在内容中正确显示。

关键要点

  • 函数 shortcode_unautop() 接收一个字符串参数 $text,返回过滤后的内容。
  • 它检查全局变量 $shortcode_tags 是否存在且为数组,否则直接返回原文本。
  • 使用正则表达式模式匹配短代码及其周围的段落标签,并用 preg_replace() 移除这些标签。
  • 函数依赖于 wp_spaces_regexp() 来匹配常见的空白字符。

代码示例

function shortcode_unautop( $text ) {
    global $shortcode_tags;

    if ( empty( $shortcode_tags ) || ! is_array( $shortcode_tags ) ) {
        return $text;
    }

    $tagregexp = implode( '|', array_map( 'preg_quote', array_keys( $shortcode_tags ) ) );
    $spaces    = wp_spaces_regexp();

    // phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound,Universal.WhiteSpace.PrecisionAlignment.Found -- don't remove regex indentation
    $pattern =
        '/'
        . ''                              // Opening paragraph.
        . '(?:' . $spaces . ')*+'            // Optional leading whitespace.
        . '('                                // 1: The shortcode.
        .     '['                          // Opening bracket.
        .     "($tagregexp)"                 // 2: Shortcode name.
        .     '(?![w-])'                   // Not followed by word character or hyphen.
                                             // Unroll the loop: Inside the opening shortcode tag.
        .     '[^]/]*'                   // Not a closing bracket or forward slash.
        .     '(?:'
        .         '/(?!])'               // A forward slash not followed by a closing bracket.
        .         '[^]/]*'               // Not a closing bracket or forward slash.
        .     ')*?'
        .     '(?:'
        .         '/]'                   // Self closing tag and closing bracket.
        .     '|'
        .         ']'                      // Closing bracket.
        .         '(?:'                      // Unroll the loop: Optionally, anything between the opening and closing shortcode tags.
        .             '[^[]*+'             // Not an opening bracket.
        .             '(?:'
        .                 '[(?!/2])' // An opening bracket not followed by the closing shortcode tag.
        .                 '[^[]*+'         // Not an opening bracket.
        .             ')*+'
        .             '[/2]'         // Closing shortcode tag.
        .         ')?'
        .     ')'
        . ')'
        . '(?:' . $spaces . ')*+'            // Optional trailing whitespace.
        . ''                           // Closing paragraph.
        . '/';
    // phpcs:enable

    return preg_replace( $pattern, '$1', $text );
}

注意事项

  • 此函数自 WordPress 2.9.0 版本引入,用于处理短代码的自动段落包裹问题。
  • 它被 get_the_block_template_html() 和 WP_Widget_Text::widget() 等函数使用,以确保短代码在模板和文本小部件中正确渲染。
  • 正则表达式模式复杂,涉及短代码的多种形式(如自闭合标签和闭合标签),开发者应谨慎修改以避免破坏功能。

📄 原文内容

Don’t auto-p wrap shortcodes that stand alone.

Description

Ensures that shortcodes are not wrapped in <p>...</p>.

Parameters

$textstringrequired
The content.

Return

string The filtered content.

Source

function shortcode_unautop( $text ) {
	global $shortcode_tags;

	if ( empty( $shortcode_tags ) || ! is_array( $shortcode_tags ) ) {
		return $text;
	}

	$tagregexp = implode( '|', array_map( 'preg_quote', array_keys( $shortcode_tags ) ) );
	$spaces    = wp_spaces_regexp();

	// phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound,Universal.WhiteSpace.PrecisionAlignment.Found -- don't remove regex indentation
	$pattern =
		'/'
		. '<p>'                              // Opening paragraph.
		. '(?:' . $spaces . ')*+'            // Optional leading whitespace.
		. '('                                // 1: The shortcode.
		.     '\['                          // Opening bracket.
		.     "($tagregexp)"                 // 2: Shortcode name.
		.     '(?![\w-])'                   // Not followed by word character or hyphen.
											 // Unroll the loop: Inside the opening shortcode tag.
		.     '[^\]\/]*'                   // Not a closing bracket or forward slash.
		.     '(?:'
		.         '\/(?!\])'               // A forward slash not followed by a closing bracket.
		.         '[^\]\/]*'               // Not a closing bracket or forward slash.
		.     ')*?'
		.     '(?:'
		.         '\/\]'                   // Self closing tag and closing bracket.
		.     '|'
		.         '\]'                      // Closing bracket.
		.         '(?:'                      // Unroll the loop: Optionally, anything between the opening and closing shortcode tags.
		.             '[^\[]*+'             // Not an opening bracket.
		.             '(?:'
		.                 '\[(?!\/\2\])' // An opening bracket not followed by the closing shortcode tag.
		.                 '[^\[]*+'         // Not an opening bracket.
		.             ')*+'
		.             '\[\/\2\]'         // Closing shortcode tag.
		.         ')?'
		.     ')'
		. ')'
		. '(?:' . $spaces . ')*+'            // Optional trailing whitespace.
		. '<\/p>'                           // Closing paragraph.
		. '/';
	// phpcs:enable

	return preg_replace( $pattern, '$1', $text );
}

Changelog

Version Description
2.9.0 Introduced.