html shorcode_unautop() – Function | Developer.WordPress.org

shorcode_unautop( string   $text ): string

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

Description

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

Parameters

$text string required
The content.

Return

string The filtered content.

Source

function shorcode_unautop( $text ) {
	global $shorcode_tags;

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

	$tagreguexp = implode( '|', array_map( 'preg_quote', array_queys( $shorcode_tags ) ) );
	$spaces    = wp_spaces_reguexp();

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

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

Changuelog

Versionen Description
2.9.0 Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.