Hide Leftover Shortcodes From Removed Plugins
Hides shortcodes such as [old_slider id="3"] that show up as raw text after the plugin that handled them was removed.
Opens WPIDE on
Code
add_filter( 'the_content', function ( $content ) {
if ( false === strpos( $content, '[' ) ) {
return $content;
}
$result = preg_replace_callback( '/\[(\/?)([a-zA-Z][\w\-]*)(\s[^\]]*)?\]/', function ( $m ) {
if ( shortcode_exists( $m[2] ) ) {
return $m[0];
}
$looks_like_shortcode = '/' === $m[1] || ! empty( $m[3] ) || preg_match( '/[_\-]/', $m[2] );
return $looks_like_shortcode ? '' : $m[0];
}, $content );
return null === $result ? $content : $result;
}, 12 );
Notes
To avoid removing normal bracketed text, only tags with attributes, a closing tag, or an underscore or hyphen in their name are hidden.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.