Is there a straight-forward way to iterate over all shortcodes in a post?

I’m working with a shortcode [translate lang=eng]Apple[/translate] which will be shown only if the “current language” is eng. If the post contains, in addition to the above, the declaration [translate lang=rus]Яаблоко[/translate] and the current language is neither rus nor eng, neither of Яаблоко or Apple is printed.

My objective is to print the message in the default language when current language does not match that of the [translate lang] attribute. This implies that the message (in the default language) will be printed as many times as [translate] is used in the post, but that’s for me to worry about…

Read More

What I need to know is if there is a get_shortcodes() function what will give me an array (or whatever) of all shortcodes in the post. Would I have to parse the post with regex if not? Do you have better suggestions?

Please refrain from telling me about “superior” translation plugins.

Related posts

Leave a Reply

2 comments

  1. That’s the best I can come up with:

    function wpse20389_show_shortcode_tags()
    {
        echo '<pre>Shortcode Tags: ';
            print_r( $GLOBALS['wp_filter']['the_content'] );
        echo '</pre>';
    }
    add_action( 'shutdown', 'wpse20389_show_shortcode_tags' );
    
  2. there is a get_shortcode_regex() function in wp-include/shortcodes.php. It is used by do_shortcode() and others. It’s fairly easy to adopt the regex expression.

    global $post;
    
    $tags = array();
    $pattern = '(.?)[(translate)bs+(.*?)(?:(/))?](?:(.+?)[/translate])?(.?)';
    $pattern = "/$pattern/s";
    preg_match_all($pattern, $post->post_content, &$tags, PREG_SET_ORDER);
    
    foreach ($tags as $t ){
    
    // iterate over tags... 
    
    }