Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress strip single shortcode from posts

I want to strip just the [gallery] shortcodes in my blog posts. The only solution I found is a filter that I added to my functions.

function remove_gallery($content) {
  if ( is_single() ) {
    $content = strip_shortcodes( $content );
  }
  return $content;
}
add_filter('the_content', 'remove_gallery');

It removes all shortcodes including [caption] which I need for images. How can I specify a single shortcode to exclude or include?

like image 747
CyberJunkie Avatar asked Feb 25 '12 01:02

CyberJunkie


Video Answer


1 Answers

To remove only the gallery shortcode , register a callback function that returns an empty string:

add_shortcode('gallery', '__return_false');

But this will only work with the callbacks. To do it statically, you can temporarily change the global state of wordpress to fool it:

/**
 * @param string $code name of the shortcode
 * @param string $content
 * @return string content with shortcode striped
 */
function strip_shortcode($code, $content)
{
    global $shortcode_tags;

    $stack = $shortcode_tags;
    $shortcode_tags = array($code => 1);

    $content = strip_shortcodes($content);

    $shortcode_tags = $stack;
    return $content;
}

Usage:

$content = strip_shortcode('gallery', $content);
like image 145
hakre Avatar answered Oct 07 '22 01:10

hakre