2

I want to know when a tweet is loaded. I have this kind of post http://pego-design.com/remarky-brisi/media-twitter-embeds/ or here http://en.support.wordpress.com/twitter/twitter-embeds/ and would like to know the height of the strange iframe create by this code :

<div class="entry-content">
<blockquote class="twitter-tweet" width="550"><p>Really cool to read through and find so much awesomeness added to WordPress 3.6 while I was gone. I should take three weeks off more often.</p>
<p>&mdash; Andrew Nacin (@nacin) <a href="https://twitter.com/nacin/status/319508408669708289">April 3, 2013</a></p></blockquote>
<p><script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script></p>

ugly with js demo : http://jsfiddle.net/qWHc5/2/

If you check this demo, you can see first and final size of the iframe (93,213,213,213… or 93,93,93,213,213…), I need to know how to get the last value without an ugly solution.

I try this How can I detect whether an iframe is loaded? but doesn't work…

UPDATE : I found this version of widgets.js https://gist.github.com/johan/4479186 with callbacks I don't knwo how to use, like this window.twttr.tfw.callbacks.cb0.

Community
  • 1
  • 1
benoît
  • 1,473
  • 3
  • 13
  • 31

2 Answers2

2

WordPress uses oEmbed to include twitter among others.
You can find the code that does this at wp-includes/class-oembed.php

To influence it use:

function so_17151843_embed ( $provider, $url, $args ) {
  // check $provider if it is twitter
  // change url values to what you want
  $url = add_query_arg( 'maxheight', $url )

  return $url;
}
add_filter( 'oembed_fetch_url', 'so_17151843_embed' );

You can place this in you funtions.php.
This should help you on your way.

janw
  • 6,672
  • 6
  • 26
  • 45
  • shortanwser, no. Why are trying to do this in js? php is safer to to this. Is there a specific reason you want to use js? – janw Jun 28 '13 at 09:32
  • how php can get the final height ? I make an update : http://jsfiddle.net/qWHc5/1/ – benoît Jun 28 '13 at 17:55
  • I don't get you, why are you showing this html. If you paste a tweet in the WordPress editor it will automaticly embed it. That's what my example helps with. – janw Jun 29 '13 at 07:45
  • When I speak of loading is to know the final height of the generated iframe. I tagged WordPress because the code is generated by WordPress but this is a js question. – benoît Jun 29 '13 at 17:04
  • you want to know the heigt of the generatd iframe? You could just get that with jQuery quite simple. So I guess there is more to it... – janw Jun 29 '13 at 19:33
  • May I ask why you need this knight? – janw Jun 30 '13 at 08:19
  • I work on an advanced grid and I need to understand this for my knowledge, I'm curious. – benoît Jun 30 '13 at 10:41
  • I found something using callback in twitter widget.js http://jsfiddle.net/qWHc5/5/ that's the best I can do… – benoît Jun 30 '13 at 20:02
-1

Here's the correct way to check when Twitter is ready:

if (window.twttr !== undefined) {
    window.twttr.ready(function(){
        console.log("Twitter ready!");
    });
};

Late answer but hope this helps future users!

punkbit
  • 7,347
  • 10
  • 55
  • 89
  • Not good, see my jsfiddle… jsfiddle.net/qWHc5/1 and your code http://jsfiddle.net/qWHc5/10/ – benoît Oct 07 '13 at 18:21
  • Yeah, how would this work in the twttr == undefined case - as before widgets.js has loaded? It won't and that's the point of the Q. – Julian Nov 12 '14 at 22:45
  • @Julian the code above works to check when the tweet is loaded. Now based on your implementation, your widgetsjs when loaded should have a callback function. Depends totally in your implementation. – punkbit Nov 13 '14 at 10:47
  • @punkbit, thanks. My implementation is a chrome extension that wants to "wake up" when a tweet is on the page with it. It isn't creating the tweet or loading widgets.js, so has no good way to attach onload to the script tag. Looking for something like window.addEventListener('twitter', ..). For now, I'm polling to see when twttr is defined, but that's ugly. – Julian Nov 13 '14 at 19:35