Extract the first oembed url inserted on the content of a post

I assume that you’re only interested in the first URL that actually succeeds at discovering actual oembed data. The oembed system processes all links it finds, but not every link will have oembed going for it, obviously.

The filter you’ll want to use is embed_oembed_html and it gets the HTML cached by oembed, the url, any attributes on the embed, and the post_ID, which is important for your code.

add_filter('embed_oembed_html', 'my_function',10,4);
function my_function( $cache, $url, $attr, $post_ID ) {
  global $my_previous_post_id;
  if ($my_previous_post_id != $post_ID) {
    // post ID changed, so this is the first oembed for the post
    // do something with $url
    $my_previous_post_id = $post_ID;
  }
  return $cache; // it's important that you return the $cache value as-is
}

Now, the whole oembed system is running at the same time as shortcodes do: during the_content filter call. So if you want to grab stuff for the header, you’ll have to start the main Loop in the header, run the_content filter over the get_the_content() value, then call rewind_posts() to rewind the query back to the start for the actual main Loop later on in the page.

This sort of behavior causes problems with plugins (like Nextgen gallery) that do stupid things when you run a loop in the header. There’s no working around it, but the fact is that those plugins are fundamentally broken and you can’t correct their problems. I get this sort of report with SFC-Share and SFC-Like all the time (because they pull content out to put in the header too). Nothing you can do about it, frankly.

Leave a Comment