I would temporarily remove a shortcode, make something and restore it… it’s possible?

// override default wordpress "the_content" hook
remove_filter('the_content', 'do_shortcode', 11);
add_filter('the_content', 'my_the_content', 11);

function my_the_content($content) {

    remove_shortcode('some_shortcode');

    $content = do_shortcode($content);

    // restore the shortcode
    add_shortcode('some_shortcode', '?????????????????????????????');

    return $content;
}

the problem is how restore it correctly…

original shortcode is in a class, eg.:

class foo {

    function __construct(){
        add_shortcode('some_shortcode', array($this, 'get_some_shortcode'));
    }

    public function get_some_shortcode(){
        return 'bar';
    }
}

1 Answer
1

Another old unanswered question. Hopefully useful to someone in the future.

WordPress stores shortcode tags and callbacks in the $shortcode_tags. This is how I’d do it.

function my_the_content( $content ) {
  global $shortcode_tags;

  $tag= 'some_shortcode';

  //* Make sure it's actually a valid shortcode
  if( ! isset( $shortcode_tags[ $tag ] ) ) {
    return $content;
  }

  $func = $shortcode_tags[ $tag ];
  remove_shortcode( $tag );

  //* Do something useful

  //* Restore the shortcode
  add_shortcode( $tag, $func );

  return $content;
}

Leave a Reply

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