1

I do not want to display the titles of videos I embed on my wordpress site. The way I've been doing this is using the embed link, inserting the iframe in the wordpress post and adding the custom Parameter "showinfo=0" to the end of the url. This has worked well, until I decided to change the design of my site. I have had to use some custom css to change all the videos (almost 200) that I've done that to in order to fit the new design. If I would have known of a way just to drop the link into wordpress (like most people do) I wouldn't have had to use an iframe and wordpress would have adjusted the size of the video based on the setting entered in the media section.

I'm hoping there is a better way to embed a Youtube video than the way I've been doing it (It would be great to just drop the link into wordpress instead of having to go through so many different steps in order to get the video to show up. Does anyone have any ideas how (of if it's even possible) to do this?

I post a video every day, so it would really help my process if there was a way to do this. Thanks.

`

<?php
load_theme_textdomain('standardtheme', get_template_directory() . '/lang');
$locale = get_locale();
$locale_file = get_template_directory() . '/languages/$locale.php';
if(is_readable($locale_file)):
    require_once($locale_file);
endif;
add_theme_support('post-thumbnails');
require_once('admin/functions.php');
require_once('lib/standardtheme.php');
require_once('lib/pro-photo/pro-photo.php');

function Oembed_youtube_no_title($html,$url,$args){
    $url_string = parse_url($url, PHP_URL_QUERY);
    parse_str($url_string, $id);
    if (isset($id['v'])) {
        return '<iframe width="'.$args['width'].'" height="'.$args['height'].'" src="http://www.youtube.com/embed/'.$id['v'].'?rel=0&showinfo=0" frameborder="0" allowfullscreen></iframe>';
    }
    return $html;
}
add_filter('oembed_result','Oembed_youtube_no_title',10,3);

?>
3
  • Out of curiosity: why can't you just drop the URL in the post, and let oEmbed handle it? Does the video URL not include the necessary query parameters? Feb 22, 2012 at 22:54
  • if you just drop the url into the post I don't see a way to prevent the title from being a displayed. I would really like to do this. Do you know if there is a way? That was my question.
    – Rick Smith
    Feb 22, 2012 at 23:12
  • It works just fine for me. Just pull the URL out of the iframe embed code, and add &showinfo=0 to the end of it. The video embed renders with no title displayed. Feb 22, 2012 at 23:35

3 Answers 3

5

No need for a plugin, You can simply use the Oembed class's oembed_result filter hook

like this:

function Oembed_youtube_no_title($html,$url,$args){
    $url_string = parse_url($url, PHP_URL_QUERY);
    parse_str($url_string, $id);
    if (isset($id['v'])) {
        return '<iframe width="'.$args['width'].'" height="'.$args['height'].'" src="http://www.youtube.com/embed/'.$id['v'].'?rel=0&showinfo=0" frameborder="0" allowfullscreen></iframe>';
    }
    return $html;
}
add_filter('oembed_result','Oembed_youtube_no_title',10,3);

so just paste this code in your theme's functions.php file, setup the width and height at the settings >> media panel and you should be just fine with simply pasting the youtube's video url in the post.

13
  • Wow, thanks! This is awesome! Do I need to go back and clean up all my old urls instead of keeping them in the iframe you think?
    – Rick Smith
    Feb 22, 2012 at 23:31
  • nope they will work as they are and new videos just drop the link
    – Bainternet
    Feb 23, 2012 at 1:00
  • Do scripts like these cause extra load time? (for instance like using more plug-ins?)
    – Rick Smith
    Feb 23, 2012 at 4:26
  • 1
    Not really, its a simple filter that runs whenever you try to read the post for the first time since Oembed has it's way of dealing with caching.
    – Bainternet
    Feb 23, 2012 at 6:39
  • Ok, cool. But it will work fine with Super-Cache?
    – Rick Smith
    Feb 23, 2012 at 13:06
3

Great solution Bainternet,

A quick addition to it, it may be a good idea to first check that the embed is a YouTube embed before replacing those values so that you don't break other embed types. It's also handy to use the WordPress pattern for parsing arguments with defaults. Based on these two additions, here's a modified function (where "namespace" is your namespace:

/**
 * Add Custom Parameters to YouTube Embeds
 */
function namespace_oembed_youtube_no_title( $html, $url, $args ) {

    // Only run this for YouTube embeds
    if ( !strstr($url, 'youtube.com') )
        return $html;

    // Get embed URL
    $url_string = parse_url($url, PHP_URL_QUERY);
    parse_str($url_string, $id);

    // Set default arguments
    $defaults = array(
        'width' => 480,
        'height' => 385,
        'showinfo' => true,
        'rel' => true
    );

    // Merge args with defaults
    $args = wp_parse_args( $args, $defaults );

    // Define variables
    extract( $args, EXTR_SKIP );

    // Add custom parameter values to IFRAME
    if ( isset($id['v']) ) {
        return '<iframe width="' . intval($width) . '" height="' . intval($height) . '" src="http://www.youtube.com/embed/' . $id['v'] . '?rel=' . intval($rel) . '&showinfo=' . intval($showinfo) . '" frameborder="0" allowfullscreen></iframe>';
    }

    return $html;
}
add_filter('oembed_result', 'namespace_oembed_youtube_no_title', 10, 3);

And now for the wp_oembed_get() call:

// oEmbed Video
$video = wp_oembed_get('http://www.youtube.com/watch?v=mx4xeO3Xq7g', array(
    'width' => 632,
    'height' => 474,
    'showinfo' => false,
    'rel' => false
) );

Thanks again for adding this solution, the original post solved an issue for me and was really helpful.

0

Just grab the URL, add your query string, and let WordPress' built-in oEmbed support handle it.

That is: pull the URL out of the iframe embed code, and then append &showinfo=0 to the end of it. Paste the result into the post content on its own line, and voila: WordPress will embed the video properly, with no title displayed.

Here's a working example: http://www.youtube.com/v/GrfGGzRNC4U?version=3&hl=en_US&showinfo=0

I pulled that URL from the embed code, not the shortened URL.

9
  • Is there a way just to use the url in the browser?
    – Rick Smith
    Feb 22, 2012 at 23:45
  • I don't understand your question. Feb 22, 2012 at 23:52
  • Sorry, You said you pulled that url from the embed code, are you tailing about the iframe code? If you were just watching a video on youtube could you just copy and past that link?
    – Rick Smith
    Feb 22, 2012 at 23:59
  • "Sorry, You said you pulled that url from the embed code, are you tailing about the iframe code?" - My exact words: "That is: pull the URL out of the iframe embed code, and then append &showinfo=0 to the end of it. " So, yes: I'm talking about the iframe embed code. Feb 23, 2012 at 0:01
  • Ok, I'm wondering if you can just add that on to the main url (that appears in your browser when viewing the video.)
    – Rick Smith
    Feb 23, 2012 at 0:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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