Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Display Google Trends embed in post WordPress

  • SOLVED

Wordpress strips the "src" in this Google Trends embed code:

<script type="text/javascript" src="http://www.google.com/trends/embed.js?hl=en-US&q=Tax+Advice,+Tax+Strategy,+Tax+Help,+Tax+Tips&geo=US&cmpt=q&content=1&cid=TIMESERIES_GRAPH_0&export=5&w=500&h=330"></script>

How can I get it to work? I don't want to use a plugin but don't mind updating my functions.php file. I'm running WP 2.9.2 (I know, I know.)

Answers (2)

2012-10-16

Dbranes answers:

you can add this shortcode into your functions.php and type [trend] in your post/page:

function my_trend(){
ob_start();
?>
<script type="text/javascript" src="http://www.google.com/trends/embed.js?hl=en-US&q=Tax+Advice,+Tax+Strategy,+Tax+Help,+Tax+Tips&geo=US&cmpt=q&content=1&cid=TIMESERIES_GRAPH_0&export=5&w=500&h=330"></script>
<?php
return ob_get_clean();
}
add_shortcode("trend","my_trend");


<strong>update:</strong> added output buffer.


chizet comments:

Thanks Dbranes. I had actually tried something similar previously and am having the same issue with your code: when I click "Preview" it never loads the javascript src. But if I reload the page the JS will load properly. After a little more research I'm beginning to wonder if there is a conflict between the code WP uses to load a new window for the Preview and our code. Thoughts?


Dbranes comments:

here is another version of the [trend] shortcode where you can now use it with parameters:

[trend w="600" h="400" q="Tax+Advice,+Tax+Strategy,+Tax+Help,+Tax+Tips" geo="US"]


function my_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("trend","my_trend");


Dbranes comments:

I have tested the code in default TwentyEleven theme (in WP 3.4.2) and it's working ok in the preview (see attached screenshot)

You might then have some conflicts, do you get any javascript errors?