shortcode_unautop()WP 2.9.0

Don't auto-p wrap shortcodes that stand alone

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

1 time — 0.000067 sec (very fast) | 50000 times — 0.67 sec (very fast)

No Hooks.

Return

String. The filtered content.

Usage

shortcode_unautop( $text );
$text(string) (required)
The content.

Examples

0

#1 Demo of work

Suppose we have the shortcode short registered. Then:

$str = '
<p>[short id="54"]</p>

Text

[short id="54"]

Text

<p>
[short id="54"]
</p>

Text

<p>[short id="54"] text</p>

Shortcode with content

<p>[short align="aligncenter" width="495"]any text inside[/short]</p>
';

echo shortcode_unautop( $str );

/* We get:
[short id="54"]

Text

[short id="54"]

Text

[short id="54"]

Text

<p>[short id="54"] text</p>

Text

[short align="aligncenter" width="495"]any text inside[/short]
*/
0

#2 Which regular expression is used

An example of what regular expression results from the function (depends on the shortcodes that are currently registered) (WP version 4.4):

(?:[\r\n\t ]|\xC2\xA0| )*+(\[(embed|wp_caption|caption|gallery|playlist|audio|video|democracy|democracy_archives|download)(?![\w-])[^\]\/]*(?:\/(?!\])[^\]\/]*)*?(?:\/\]|\](?:[^\[]*+(?:\[(?!\/\])[^\[]*+)*+\[\/\])?))(?:[\r\n\t ]|\xC2\xA0| )*+<\/p>

Notes

  • Global. Array. $shortcode_tags

Changelog

Since 2.9.0 Introduced.

shortcode_unautop() code WP 6.4.3

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 );
}