strip_shortcodes()
Remove all shortcode tags from the given content.
Hooks from the function
Return
String
. Content without shortcode tags.
Usage
strip_shortcodes( $content );
- $content(string) (required)
- Content to remove shortcode tags.
Examples
#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; }
#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.
#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.
#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. |