Strip Multiple Shortcodes in WordPress

The code snippet allows you to exclude particular pages or post from running the shortcode, it simply removes the shortcode instead of displaying or running it.

You can use this code to strip out one or more shortcodes, as needed.
Just call the function dot1_strip_shortcode for a particular post or category of posts or for the whole lists of posts.

Add this code to your functions.php:

global $remove_shortcode;
/**
* Strips and Removes shortcode if exists
* @global int $remove_shortcode
* @param type $shortcodes comma seprated string, array of shortcodes
* @return content || excerpt
*/
function dot1_strip_shortcode( $shortcodes ){
global $remove_shortcode;
if(empty($shortcodes)) return;

if(!is_array($shortcodes)){
$shortcodes = explode(',', $shortcodes);
}
foreach( $shortcodes as $shortcode ){
$shortcode = trim($shortcode);
if( shortcode_exists($shortcode) ){
remove_shortcode($shortcode);
}
$remove_shortcode[$shortcode] = 1;
}
add_filter( 'the_excerpt', 'strip_shortcode' );
add_filter( 'the_content', 'strip_shortcode' );
}
function strip_shortcode( $content) {
global $shortcode_tags, $remove_shortcode;

$stack = $shortcode_tags;
$shortcode_tags = $remove_shortcode;
$content = strip_shortcodes($content);

$shortcode_tags = $stack;
return $content;
}

Call the function dot1_strip_shortcode(), with proper arguments as per your condition.

e.g. :

dot1_strip_shortcode( 'Test' );
dot1_strip_shortcode( 'Test, new_shortcode' );
dot1_strip_shortcode( array('Test', 'new_shortcode') );

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.