X

How To Create a Shortcode to Embed a Google Trends Graph in Posts

Snippets by IsItWP

Are you looking for a way to create a shortcode that embeds a Google Trends graph in posts and pages? This Google Trends shortcode will embed a graph of any comma separated query you enter to display a trend over time.

Instructions:

  1. Add this code to your theme’s functions.php file or in a site-specific plugin.
  2. Be sure to add a + sign between multiple words that are part of a single query.
  3. By default, the geolocation is set to the United States, so be sure to change the geo attribute if you need it to be set to another location.
  4. Here is an example shortcode: [trends h="450" w="500" q="wordpress,+wordpress+theme,+wordpress+plugin,+wordpress+snippets" geo="US"]
function wps_trend($atts){
        extract( shortcode_atts( array(
                'w' => '500',           // width
                'h' => '330',           // height
                'q' => '',              // query
                'geo' => 'US',          // geolocation
        ), $atts ) );

        //format input
        $h=(int)$h;
        $w=(int)$w;
        $q=esc_attr($q);
        $geo=esc_attr($geo);
         ob_start();
?>
<script type="text/javascript" src="http://www.google.com/trends/embed.js?hl=en-US&q=<?php echo $q;?>&geo=<?php echo $geo;?>&cmpt=q&content=1&cid=TIMESERIES_GRAPH_0&export=5&w=<?php echo $w;?>&h=<?php echo $h;?>"></script>
<?php
return ob_get_clean();
}
add_shortcode("trends","wps_trend");

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 9 Best SEO Tools to Grow Your Website Traffic, FAST!

Comments   Leave a Reply

  1. Can you update this to work with Trends update?

    I’ve been trying, but it is annoying.

    Also so that you can use it twice on one wp post/page, which seems to be a problem.

  2. Hello Debjit, thank you for the tutorial. It really helps!

    However, is there any way to only providing trends statistic for the past 5 years?

    Any help would be appreciated!

  3. disqus_guEqGGv6i5 September 30, 2014 at 7:10 am

    fv

  4. How could I dynamic a organogram chart for a organisation…

    1. Could you please provide more details about what you are trying to do?

  5. Best WordPress snippets, hacks and tips from 2013 | CatsWhoCode.com December 30, 2013 at 11:00 am
  6. WordPress shortcode to embed Google trends graphs June 24, 2013 at 11:46 am

    […] to Kevin Chard for the […]

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!