Hot answers tagged

16

The embed_oembed_html filter runs before an oEmbed resource's HTML is outputted, so you could hook into this and wrap the output in a div as below. I can't think of a simple way of wrapping the other content. add_filter('embed_oembed_html', 'my_embed_oembed_html', 99, 4); function my_embed_oembed_html($html, $url, $attr, $post_id) { return '<div id="...


12

You can use the get_media_embedded_in_content() function for both shortcode and embedded media (it looks for these HTML tags: audio, video, object, embed, or iframe), just be sure to use content with applied filters and shortcode executed. For example: $post_id = 125; $post = get_post($post_id); //Get the content, apply filters and execute shortcodes $...


10

Note that the WPCS standards for PHPCS are not "official". I am one of the maintainers, and all that we can do is to do our best to match the standards that WordPress suggests. In this case, I'm unsure how you would escape the output from wp_oembed_get(). The function may indeed need to be escaped as the returned value is passed through several filters, and ...


9

All you need to do use apply_filters. foreach ($video as $vid) { echo '<li>'.apply_filters('the_content',$vid).'</li>'; } It may be more efficient to concatenate a string and then run the filter on the whole thing. $lis = ''; foreach ($video as $vid) { $lis .= '<li>'.$vid.'</li>'; } echo apply_filters('the_content',$lis); ...


9

I needed just this for a recent project, so here's my plugin! The code should be self-explanatory, but if there are any questions ask away. <?php /** * Plugin Name: oEmbed Featured Image * Plugin URI: http://wordpress.stackexchange.com/q/70752/1685 * Description: Automatically set the featured image if an oEmbed-compatible embed is found in the post ...


9

How to trigger an oembed cache regeneration The default cache time is 24 hours and we can adjust it with the oembed_ttl filter. But as you've noticed, expired cache is not enough to trigger a cache regeneration. The reason is this line in the WP_Embed class: if ( $this->usecache || $cached_recently ) { so to trigger a regeneration, we need both ...


7

I'm assuming you are using WordPress' Embeds feature. Generally speaking wp_embed_defaults() gives you the information you need. A look at the source discloses the possibilities you have: 2017 /** 2018 * Create default array of embed parameters. 2019 * 2020 * The width defaults to the content width as specified by the theme. If the 2021 *...


6

You can try this: add_filter( 'embed_oembed_html', 'custom_youtube_oembed' ); function custom_youtube_oembed( $code ){ if( stripos( $code, 'youtube.com' ) !== FALSE && stripos( $code, 'iframe' ) !== FALSE ) $code = str_replace( '<iframe', '<iframe class="youtube-player" type="text/html" ', $code ); return $code; } to target ...


6

The accepted answer didn't work in my case. URLs were still being converted to embeds in the post editor. By looking at the source of wp-includes/class-wp-embed.php where this stuff is handled, I found that there are some other things at play here: // Hack to get the [embed] shortcode to run before wpautop() add_filter( 'the_content', array( $this, '...


6

Actually this was quite easy – when you know what's missing: The current post ID for the global $wp_embed object, so it knows what to refer to. The reason is simple: oEmbeds get cached as post meta data, so without knowing the ID, the MarkUp can't get fetched and replaced in the content. // grab a post from the database /** @var \WP_Embed $wp_embed */ ...


6

$url will contain the full URL to the embed source. E.g.: https://www.youtube.com/watch?v=_UmOY6ek_Y4, so you have to search within $url to see if the provider's name appears: add_filter( 'embed_oembed_html', 'wpse_embed_oembed_html', 99, 4 ); function wpse_embed_oembed_html( $cache, $url, $attr, $post_ID ) { $classes = array(); // Add these ...


5

Notes Solution for YouTube and Vimeo. Uses Featured Image or default thumbnail from video provider. If more than one oEmbed exists in the same page, using the Featured Image provoques duplicate "thumbs". Post has to be updated before changes are seen. To do: <iframe> and <img> sizes, other oEmbeds. Sources of Inspiration oEmbed oEmbed, ...


5

The problem: The reason why this isn't working, is this part of the WP_Embed::shortcode() method: if( $post_ID ) { .... cut ... // Use oEmbed to get the HTML $html = wp_oembed_get( $url, $attr ); ... cut ... } When trying to autoembed with the Heartbeat API the $post_ID is null, so wp_oembed_get() is never activated. No Caching: When ...


5

Something like this should do the trick and force rel=0 for all YouTube oembed results. add_filter('oembed_dataparse','youtube_force_rel',10,3); function youtube_force_rel($return, $data, $url) { if ($data->provider_name == 'YouTube') { return str_replace('feature=oembed', 'feature=oembed&#038;rel=0', $return); } else { return ...


5

The class-oembed.php file reveals some of the available filters regarding the oEmbeds. We can use the oembed_result or oembed_dataparse filters to modify the HTML fetched from Vimeo before it's cached in the post meta. Here's an example for the latter: add_filter( 'oembed_dataparse', function( $return, $data, $url ) { // Target only Vimeo: if( ...


4

I know this question is pretty old, but I have a working solution that I figure might be helpful to anyone else trying to find this info and coming up empty in Google searches. This solution has been tested in WordPress 3.7.1+: /** * Adds the `autoplay` query string argument to embedded YouTube videos */ function wpse5362_autoplay_youtube_oembed( $...


4

You can filter the HTML output for oEmbed with oembed_result. Now test the HTTP host of the URL for www.youtube.com and add the parameter. The oEmbed result is cached in a post meta field to avoid too many requests. To update old posts I have added an activation helper that clears those cached content for Youtube embeds only. <?php # -*- coding: utf-8 ...


4

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 WP autoembed function. Here is the code, as seen in wp-includes/class-wp-embed.php: /** * Passes any unlinked URLs that are on their own line to {@link WP_Embed::shortcode()} for potential ...


4

Another method can be like this, On the current post when you save look for youtube video in the content, if found save it to the transient 'videos' and the post_id (for the link to the post later) insted of regex im using parse_url() and parse_str() add_action( 'save_post', 'save_youtube_videos' ); function save_youtube_videos( $post_id ) { $saved = ...


4

Wordpress does a pretty good job of embedding videos at the correct aspect ratio. The width of the embedded video is based on the content width set in your theme's functions.php file. Because of this it sounds as if there may be some styles applied to the iframe element in your theme changing it's size. To ensure that your videos work at any width within a ...


4

Without knowing more about your theme I can only hazard a few guesses. What are you inputting as an excerpt, what is the current output you are seeing for your excerpt, and what is the expected output? Are you seeing the URL show up in the excerpt, or is it being stripped? Oembed Are you being sure to scope in $wp_embed? global $wp_embed; add_filter( '...


4

Make a custom plugin for your site, put this code in it. add_filter('oembed_dataparse','oembed_youtube_add_wrapper',10,3); function oembed_youtube_add_wrapper($return, $data, $url) { if ($data->provider_name == 'YouTube') { return "<div class='whatever'>{$return}</div>"; } else { return $return; } } Note: The ...


4

Personally, I found the oembed_dataparse filter pretty fiddly to use; sometimes it worked, sometimes it didn't; and when used in conjunction with custom TinyMCE instances, it seems as though the added wrapper was hard-baked into the content rather than added via the filter at output. I found the embed_oembed_html filter much more reliable and works every ...


4

Instagram has changed its embed code from being just an iframe to a bunch of HTML and a JS script. Very inelegant, but nothing we can do. This setup, of course, fails when called through AJAX since the JS file that's part of the HTML does not run. Thankfully there is another official way to make it work with AJAX in two steps: Include this scripts in your ...


4

if you could make it to something like this: <div class="videoWrapper"> <iframe width="600" height="338" src="https://www.youtube.com/embed/f5CcOq8UzkI?feature=oembed" frameborder="0" allowfullscreen=""></iframe> </div> then responsive css for that would be like this: .videoWrapper { position: relative; padding-bottom: ...


4

Pretty sure you can just remove them entirely: remove_action( 'embed_content_meta', 'print_embed_comments_button' );


4

WordPress doesn't add the frameborder attribute. YouTube does. It's part of their embed code. If you absolutely must remove it you can use the embed_oembed_html filter to modify the returned HTML: function wpse_308247_remove_frameborder( $html, $url ) { // If the URL to be embedded is from YouTube. if ( strpos( $url, 'youtube.com' ) !== false ) { ...


3

Without even running that code I can see a pretty significant problem. You describe the goal as to "check all my posts to see if there is a embedded YouTube video" but the query is restricted to only one post. And then, you are only attempting to use the very first post in the results (if the query works at all) anyway so there is no way that this is going ...


3

Building a fake embed shortcode and then calling run_shortcode to do this makes no real sense. Try this instead: echo $wp_embed->shortcode(array(), $my_tour_video); Edit: Additionally, when I use your code on a test page, using that embed, I get no such error as you're describing. If you can come up with a reproducible test case, then it might be ...


3

Just use the [embed] shortcode around your items. <ul> <li>[embed]http://www.youtube.com/link/to/content[/embed]</li> ...


Only top voted, non community-wiki answers of a minimum length are eligible