How to detect if a Shortcode is being called on a page?

I’m trying to push some variables to google analytics and would like to be able to detect if a shortcode has been included on a page at all.

Anybody know a smart way going about this?

Read More

Thank you!

Noel

Related posts

Leave a Reply

4 comments

  1. I have sometimes wondered the same thing – whether wp maybe checked on save and kept a register etc

    I had a quick look at the code, and it seems not.

    There is however a global $shortcode_tags, and wp has this function

    function get_shortcode_regex() {
        global $shortcode_tags;
        $tagnames = array_keys($shortcode_tags);
        $tagregexp = join( '|', array_map('preg_quote', $tagnames) );
    
        // WARNING! Do not change this regex without changing do_shortcode_tag() and strip_shortcodes()
        return '(.?)[('.$tagregexp.')b(.*?)(?:(/))?](?:(.+?)[/2])?(.?)';
    }
    

    used here to apply the shortcode:

    $pattern = get_shortcode_regex();
    return preg_replace_callback('/'.$pattern.'/s', 'do_shortcode_tag', $content);
    

    My regex is not that great, but maybe you could do something with that?

    Alternatively:

    Then there is the brute force way – for each of the shortcode tags, ‘simply’ check if '['.$tag is in the content ?
    But then some developers / older plugins do their own funky filters on the content with comments or other custom tags.

    So then, you could also check the wp global $wp_filter for $wp_filter['the_content']; the contents should the functions called for “the content”.

    To be clever

    You could maybe add an action yourself on post save/update, do the check for filters and shortcodes then and store something in a post-meta. Then all you have to do at display time is check the post-meta.

    Whew… is it worth it?

  2. So here’s my trainwreck version of it (as it stands this minute, Anmari has given plenty of good ideas). It’s not really flexible at all for anyone else’s use either, but our hosted platform and how people use it is very flexible (makes my life easier):

    function tfh_analytics() {
    
        $client_ua = trim(get_option( 'tf_ua_analytics' ));
    
        ?>
        <script type='text/javascript'>
            //<![CDATA[
            var _gaq = _gaq || [];
            // TF Aggregate Tracking
            _gaq.push(['_setAccount','UA-xxxxxxx-x']);
            _gaq.push(['_trackPageview'],['_trackPageLoadTime']);
            // Define Theme
            _gaq.push(['_setCustomVar',1,'Theme','<?php echo TF_THEME; ?>']);
            // Define Category
            <?php
            if ( have_posts() && ( is_page() || is_single() ) ) {
                $content = get_the_content(the_post());
                // Check if Front Page
                if ( is_front_page() ) { 
                    if ( is_home() ) {
                    $category = 'home-blog';
                    } else {
                    $category = 'home-static'; 
                    }
                } else {
                    // Check if Events
                    if ( stripos( $content , '[tf-event') ) { $category = 'events'; }
                    // Check if Food Menu
                    if ( stripos( $content , '[tf-food') ) { $category = 'food-menu'; }
                    // Check if Location
                    if ( stripos( $content , '[tf-googlemaps') ) { $category = 'location'; }
                    // Check if Blog
                    if ( is_home() ) { $category = 'blog'; }
                    }
                if ( !$category ) { $category = 'other'; } 
            }   
            ?>
            _gaq.push(['_setCustomVar',2,'Category','<?php echo $category; ?>']);
            // Define Yelp Bar
            _gaq.push(['_setCustomVar',3,'YelpEnabled','<?php if( get_option( 'tf_yelp_enabled' ) == true) { echo 'true'; } else { echo 'false';} ?>']);     
            // Define User Type
            _gaq.push(['_setCustomVar',4,'User','<?php if( is_user_logged_in() ) { echo 'logged_in'; } else { echo 'logged_out'; } ?>',1]);
    
        <?php if ( $client_ua != '' ) { ?>
            // Client Tracking
            _gaq.push(['b._setAccount','<?php echo $client_ua; ?>']);
            _gaq.push(['b._trackPageview']);
        <?php } ?>
    
        (function() {
            var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
            ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
            var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
            })();
            //]]>
        </script>
        <?php
    }
    
  3. A simple way, use $tag variable within your shortcode function. Get this idea from here

    add_shortcode("your_shortcode_tag", "shortcode_function");
    
    function shortcode_function($atts, $content = null, $tag){ // $tag variable is here
       // check if your shortcode is called
        if($tag == "your_shortcode_tag"){
            // do your stuff
        }
    
    }