I need to change the way WP outputs html when given an embed URL within the content. Example content within the WP editor:
This is standard content and will be wrapped in a 'p' tag. But check out this cool vid:
http://www.youtube.com/watch?v=3JJv4TvURj4
This will be output as another 'p' tag. The above URL will be turned into an iFrame.
The goal is for the client to input something simple like that, and change the way wordpress outputs it on the page. I have been using the oembed_dataparse
filter to good effect so far, but I would like to take the video URL and pass it to another function outside of the filter. How can I do that?
My initial approach was to use a global variable. Here’s my abridged filter code:
add_filter('oembed_dataparse', 'modal_embed', 10, 3);
function modal_embed($html, $data, $url) {
// This is supposed to store the URL globally so I can access it
// outside this filter.
global $video_url;
$video_url = $url;
$custom_output = "<div class='video'>$html</div>"; // Just an example.
return $custom_output;
}
The code I create as $custom_output
works just fine and is displayed on the page. The issue is that I’m not getting access to $video_url
globally as I expected. Any thoughts? Thanks.
OK, got it. I just dug around in wp core a bit and found the function they use to grab autodetcts. WP uses the php
preg_replace_callback
function within the WPautoembed
function. Here is the code, as seen inwp-includes/class-wp-embed.php
:Since i’m only planning on the client inputting one video per post I use the
preg_match
function along with this same RegEx patter to achieve what I want:This will find the first autoembed url and return it.