strip_shortcodes()WP 2.5.0

Remove all shortcode tags from the given content.

1 time — 0.000307 sec (fast) | 50000 times — 0.44 sec (very fast) | PHP 7.1.11, WP 4.9.6
Hooks from the function

Return

String. Content without shortcode tags.

Usage

strip_shortcodes( $content );
$content(string) (required)
Content to remove shortcode tags.

Examples

0

#1 Cut shortcodes from content

Let's remove the shortcodes from the texts of the home page, but don't touch any other content (single post, categories, tags).

add_filter( 'the_content', 'remove_shortcode_from_index' );

function remove_shortcode_from_index( $content ) {

	if ( is_home() ) {
		$content = strip_shortcodes( $content );
	}

	return $content;
}
0

#2 Removing a non-existent shortcode (using get_shortcode_regex())

Since version 4.4, removing shortcodes that WP knows nothing about has become easier - you don't need to write any regulars. Everything can be done with the standard functions of WordPress. And also through the filter.

Shortcodes will be removed using get_shortcode_regex(), in which you can now pass an array where you specify the name of the shortcode to be cut from the content and thus get the right regular to use it in preg_replace().

$content = "Start [asd]text[/asd] end. And one more content [myshort]text[/myshort] all."
$shortcodes_to_remove = [ 'myshort' ];

$regex   = get_shortcode_regex( $shortcodes_to_remove );
$content = preg_replace( "/$regex/", '', $content );

echo $content;

// Outputs: Start [asd]text[/asd] end. And one more content all.
0

#3 Removing a non-existent shortcode (through the filter)

It is worth to note that this function removes only those shortcodes which are registered at the moment of the processing of this function. If there are any “leftovers” shortcodes, like those which were added by some plugin but that plugin is not active anymore, this function WILL NOT strip them.

You can add a non-existing shortcode to the strip_shortcodes_tagnames filter so that this function removes the specified shortcode as well.

// add a non-existent shortcode to the array for deletion
add_filter( 'strip_shortcodes_tagnames', function( $tags_to_remove ){
	$tags_to_remove[] = 'myshort';

	return $tags_to_remove;
} );

$content = "Start [asd]text[/asd] end. And one more content [myshort]text[/myshort] all."
$content = strip_shortcodes( $content );
echo $content;

// Outputs: Start [asd]text[/asd] end. And one more content all.
0

#4 Removing a non-existent shortcode (via regular expression)

This function will not remove constructs similar to shortcodes: [asd] or [asd]...[/asd] if they are not registered in WordPress. To do this, you can use a regular expression:

$content = "1 [foo] [asd]text[/asd] 2";
$content = preg_replace( '~\[[^\]]+\]~', '', $content );
echo $content;

// Returns: 1 text 2

To cut only the desired shortcode, for example [myshort] or [myshort]text[/myshort] use this regular:

$content = "Start [asd]text[/asd] end. And another [myshort] and content [myshort]text[/myshort] all."
$content = preg_replace( '~\[/?myshort[^\]]*\]~', '', $content );
echo $content;

// will print: Start [asd]text[/asd] end. And one more and content text all.

Let's cut the "content shortcode" along with the content:

$content = "Start [asd]text[/asd] end. And one more content [myshort]text[/myshort] all."
$content = preg_replace( '~(?:\[(myshort)[^\]]*\].*?\[/\])|(?:\[myshort[^\]]*\])~s', '', $content );
echo $content;

// Outputs: Start [asd]text[/asd] end. And one more content all.

Notes

  • Global. Array. $shortcode_tags

Changelog

Since 2.5.0 Introduced.

strip_shortcodes() code WP 6.4.3

function strip_shortcodes( $content ) {
	global $shortcode_tags;

	if ( ! str_contains( $content, '[' ) ) {
		return $content;
	}

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

	// Find all registered tag names in $content.
	preg_match_all( '@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches );

	$tags_to_remove = array_keys( $shortcode_tags );

	/**
	 * Filters the list of shortcode tags to remove from the content.
	 *
	 * @since 4.7.0
	 *
	 * @param array  $tags_to_remove Array of shortcode tags to remove.
	 * @param string $content        Content shortcodes are being removed from.
	 */
	$tags_to_remove = apply_filters( 'strip_shortcodes_tagnames', $tags_to_remove, $content );

	$tagnames = array_intersect( $tags_to_remove, $matches[1] );

	if ( empty( $tagnames ) ) {
		return $content;
	}

	$content = do_shortcodes_in_html_tags( $content, true, $tagnames );

	$pattern = get_shortcode_regex( $tagnames );
	$content = preg_replace_callback( "/$pattern/", 'strip_shortcode_tag', $content );

	// Always restore square braces so we don't break things like <!--[if IE ]>.
	$content = unescape_invalid_shortcodes( $content );

	return $content;
}