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:
- Add this code to your theme’s functions.php file or in a site-specific plugin.
- Be sure to add a
+
sign between multiple words that are part of a single query. - 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. - 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!
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.
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!
fv
How could I dynamic a organogram chart for a organisation…
Could you please provide more details about what you are trying to do?
[…] Source: http://wpsnipp.com/index.php/functions-php/shortcode-embed-google-trends-graph… […]
[…] to Kevin Chard for the […]