-
-
Save mustardBees/7704269 to your computer and use it in GitHub Desktop.
<?php | |
/** | |
* Filter a few parameters into YouTube oEmbed requests | |
* | |
* @link http://goo.gl/yl5D3 | |
*/ | |
function iweb_modest_youtube_player( $html, $url, $args ) { | |
return str_replace( '?feature=oembed', '?feature=oembed&modestbranding=1&showinfo=0&rel=0', $html ); | |
} | |
add_filter( 'oembed_result', 'iweb_modest_youtube_player', 10, 3 ); |
Thank you! +1
This hack did not work for my wordpress @ indiahashtag.com
replace oembed_result with embed_oembed_html for this to work properly
Thanks for this. I made a tweak that makes it easier to build the querystring.
<?php
function imp_custom_youtube_querystring( $html, $url, $args ) {
if(strpos($html, 'youtube')!= FALSE) {
$args = [
'rel' => 0,
'controls' => 0,
'showinfo' => 0,
'modestbranding' => 1,
];
$params = '?feature=oembed&';
foreach($args as $arg => $value){
$params .= $arg;
$params .= '=';
$params .= $value;
$params .= '&';
}
$result = str_replace( '?feature=oembed', $params, $html );
}
return $result;
}
add_filter('oembed_result', 'imp_custom_youtube_querystring', 10, 3);
?>
If you have installed jetpack, you need to add this filter:
/* $html example value:
<span class="embed-youtube" style="text-align:center; display: block;"><iframe class='youtube-player' type='text/html' width='1220' height='717' src='http://www.youtube.com/embed/ZuYCXwcCNqM?version=3&rel=1&fs=1&autohide=2&showsearch=0&showinfo=1&iv_load_policy=1&wmode=transparent' allowfullscreen='true' style='border:0;'></iframe></span>
*/
function imp_custom_youtube_querystring( $html ) {
if(strpos($html, 'youtube')!= FALSE) {
$args = [
'rel' => 0,
'controls' => 0,
'showinfo' => 0,
'modestbranding' => 1,
];
$params = '?version=3&';
foreach($args as $arg => $value){
$params .= $arg;
$params .= '=';
$params .= $value;
$params .= '&';
}
$result = str_replace( '?version=3', $params, $html );
}
return $result;
}
add_filter('video_embed_html', 'imp_custom_youtube_querystring', 10, 3);
So usefull, thank you @mustardBees !
@alexstandiford thanks a lot for your code.
Unfortunately, when we have both a Youtube video and a Vimeo video on the same page, your code prevents the display of Vimeo video... I can't figure out why? Do you have an idea ?
The original code from @mustardBees does not cause any bug, though.
@MatRouault Replace all occurences of $result with $html and it should work.
@MatRouault did you find a soulution where you are able to hide controls on both youtube and vimeo videos?
How is it posible that if I have deleted the code it still appear in page source?
I tried it with a page that has a video embed. It worked.. then I deleted the code in functions.php but the video still applied the same parameters.
How was it possible? (I asked to myself)
I delete de code, deleted the cache folder... I forced an error in functions.php in order to be sure it's being updated and used... I edited the page and save it again. I deleted the video in the page and paste it again...
But the video is still using those arguments.
And it happened just with the video I have had used to test.. the other videos working fine.
I added the code again but changing one argument "controls=1".. but this video still used the other parameter. I tried other videos and they worked fine.
WHY?
I gone to youtube to get other URL for the same video: https://youtu.be instead of https://youtube.com/
I changed it and it worked fine.
Now I have this in the page:
https://youtu.be/JR06ALEyrEU
https://www.youtube.com/watch?v=JR06ALEyrEU
And I get this in the source code of the page:
<iframe width="840" height="630" src="https://www.youtube.com/embed/JR06ALEyrEU?feature=oembed&rel=0&controls=1&showinfo=0&modestbranding=1&" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
-- |<iframe width="840" height="630" src="https://www.youtube.com/embed/JR06ALEyrEU?feature=oembed&rel=0&controls=0&showinfo=0&modestbranding=1&&modestbranding=1&showinfo=0&rel=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
I have tried in other pages and If I use https://www.youtube.com/embed/JR06ALEyrEU (the 1st video I tried with) I get the OLD colde, but If I use any other video, I get the new code (with "controls=1")
It's like if wordpress... in the process of creating the video preview, cached it and do not change it. But I deleted the cache folder.
Don't you love f***ing computing ?
In case someone else comes across this and wants to change the settings for youtube embeds in Wordpress, this only works when you're adding a new video to a page not on existing embeds. In addition, this doesn't work with the way YouTube gives share URLs which uses the youtu.be domain name. This is how I altered it:
function imp_custom_youtube_querystring( $html, $url, $attr, $post_id ) {
if (strpos($html, 'youtube')!= FALSE || strpos($html, 'youtu.be') != FALSE) {
$args = [
'rel' => 0,
'controls' => 0,
'showinfo' => 0,
'modestbranding' => 1,
];
$params = '?feature=oembed&';
foreach ($args as $arg => $value) {
$params .= $arg;
$params .= '=';
$params .= $value;
$params .= '&';
}
$html = str_replace( '?feature=oembed', $params, $html );
}
return $html;
}
add_filter('embed_oembed_html', 'imp_custom_youtube_querystring', 10, 4);
One thing you should note about this is that it will run on every embed on every page load rather than have the more performant way that is suggested above. Probably not that big of a deal unless you've got a really high volume site though, in which case you're better off just removing and re-embedding the video and using the oembed_result
filter from above instead.
Useful for me!
Thanks for sharing