Become Wordpress Professional - Your Ultimate Wordpress Guide

Thursday, 18 July 2013

How to Embed Google Trends Graph in WordPress


How to Embed Google Trend Graph in WordPress
Today you will learn how to embed Google trend graph in your WordPress blog. Google trend graph is a free online service that allow users to track the popularity of specific keyword but you have to make sure to add + button between multiple words that are part of a single query. By default this snippet of code is set to a United State as a geo location but you can change it as per your needs.

Embed Google Trend Graph in Theme File

The first step is to add a snippet of code in your WordPress theme function.php file. Go to WordPress admin dashboard and then click Appearance >> Editor. Open your function.php file and at the end of all codes paste the following code :

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

Now save function.php file. The next step is to use the short-code in your post or pages where you want the Google Trends graph to be rendered. Following is the shortcode:

[trends h="450" w="500" q="wpsnipp,wordpress,+wordpress+theme,+wordpress+plugin,+wordpress+snippets" geo="US"]


The above code can be used in anywhere in your theme file, pages, post and sidebars. But Keep in mind that  if you change the default geo location that is US, then you must change US from the shortcode.

0 comments:

Post a Comment

Please Avoid Spamming. Comments will be moderated before they are published.