How to Embed Google Trends Graph in WordPress Posts And Pages

google-trends-logo

Google Trends is very nice tool to find what people are searching around the world. It helps you in finding what is treding around you. If you are writing a post, it would be impresive if you add a trend graphs to show the latest trends. Do you want to embed Google Trends graph in your WordPress blog’s post or page? Try a simple code snippet to make short code. And use Google Trends graph in post easily.

Also Read: How to Create Your Own WordPress ShortCodes

Add the below code to functions.php file

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, use this shortcode to add graph in post or page.

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

Here q is the search term for which you want to show the trend graph. By default the geolocation is set to United States. In case you want to change the location, you can change it in param while adding shortcode.

If you do not want to edit code, you can use Google Trends Shortcode plugin. It does the similar thing. After installing the plugin, you can easily embed the trend graph with same shortcode.

Source: WpSnipp

Tags: | |

Deepanker Verma is the founder of Techlomedia. He is a tech blogger, developer and gadget freak.


Similar Articles

0 Comments

Leave a comment

Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.

2020 UseThisTip | Part of Techlomedia Internet Pvt Ltd Developed By Deepanker