How to Add Google Trends Graph in Post/Page via Shortcode

x theme

Google Trends is a service provided by Google Inc. This service shows the most searched term, based on Google Search. Google also provides an API to display Google trends in any website. But we’ll implement Google Trends in WordPress by creating a shortcut for Google Trends.

Paste the following code in functions.php. The code will generate a WordPress shortcut for Google Trends to use the shortcut in your post or page.

function wps_trend($atts){
extract( shortcode_atts( array(
'w' => '500', // width
'h' => '330',// height
'q' => '',// query
'geo' => 'UK',// 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-UK&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 shortcode as:

[trends h="450" w="500" q="wpulti,wordpress,+wordpress+theme,+wordpress+plugin,+wordpress+tutorials" geo="UK"]

Related posts:

About the author

admin

Leave a Reply

Your email address will not be published. Required fields are marked *