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...

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.

  • Why can't you do it when the translate codes are dealt with by do_shortcodes? Remember that you can set a plugin-specific flag so you don't do stuff multiplt times if you don't want it to. – Raphael Feb 18 '12 at 13:28

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' );

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)\b\s+(.*?)(?:(\/))?\](?:(.+?)\[\/translate\])?(.?)';
$pattern = "/$pattern/s";
preg_match_all($pattern, $post->post_content, &$tags, PREG_SET_ORDER);

foreach ($tags as $t ){

// iterate over tags... 


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.