Barcode for Wordpress

I want to remove only short tags [gallery]in my blog posts. The only solution I found was 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 one short code to exclude or include?

+5
source share
2 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 callbacks. To do this statically, you can temporarily change the global state of wordpress to trick 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;
}

Using:

$content = strip_shortcode('gallery', $content);
+13
source

, , -

global $post;
$postContentStr = apply_filters('the_content', strip_shortcodes($post->post_content));
echo $postContentStr;
-1

All Articles