1

I'd like to suppress related YouTube videos by appending '&rel=0' to the string of embed attributes output to the video iframe in my post.

I believe this requires a filter that modifies the $attr array before it is passed to wp_embed_handler_youtube() (located in wp-includes/media.php).

Assuming I'm on the right track, is there a way to add a filter to my child theme to accomplish this?

Thanks in advance for your help!


Ok, so I'm still pretty lost... here is the code in my functions.php:

function modify_YT_embed_attributes($embed, $attr, $url, $rawattr) {  
    $attr["rel"] = "0"; //modify the attributes array
    return $embed; 
}    
add_filter('wp_embed_handler_youtube', 'modify_YT_embed_attributes', 10, 4);

Still not working. What is the obvious thing I'm missing?

2
  • as you see the file media.php, you can use the filter wp_embed_handler_youtube
    – mmm
    Aug 24, 2015 at 19:18
  • Thanks mmm, for your speedy reply. I suspected it was indeed possible. However in this case I can't find any relevant examples of the code I might use in my child theme functions.php to accomplish the change. Can you help get me started? Many thanks!
    – Bodhiji
    Aug 24, 2015 at 20:19

1 Answer 1

3

There is a filter with the same name as the function wp_embed_handler_youtube

https://developer.wordpress.org/reference/hooks/wp_embed_handler_youtube/

add_filter('wp_embed_handler_youtube', 'ehy_callback', 10, 4);

function ehy_callback($embed, $attr, $url, $rawattr){

   //make necessary changes here
   return $embed;
}

You can add the code in functions.php of your child theme.

4
  • this filter returns is applied on the HTML code. so you have to return the result of the callback in $embed. look the content of the 4 arguments to help you
    – mmm
    Aug 24, 2015 at 23:25
  • @WisdmLabs Thanks for the code example. I'm puzzled tho. In this case I'll be modifying $attr. Wouldn't I return that parameter instead? Thanks in advance for your clarification.
    – Bodhiji
    Aug 25, 2015 at 0:43
  • @mmm I looked, and I'm still awash in the fog of my own ignorance... :) If you had a look at my code above and could help out, I'd sure appreciate it!
    – Bodhiji
    Aug 25, 2015 at 0:44
  • can you try printing $embed and see if you can include necessary parameter into that. If the output is not being printed, just log it in DB and check the result.
    – Domain
    Aug 25, 2015 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.