How to have normal YouTube-links handled by WP YouTube Lyte

Although by default WP YouTube Lyte only works with httpv or httpa links and hence does not act on normal YouTube links (which are instead auto-handled by oEmbed in WordPress core), you can easily change this behavior by adding the following code-snippet to your theme’s function.php or to a separate “helper”-plugin:

/** force wp youtube lyte on http://www.youtube.com url's as well */
add_filter('the_content', 'force_lyte_parse', 1);
function force_lyte_parse($content) {
     $content=str_replace('http://www.youtube.com/watch?v=','httpv://www.youtube.com/watch?v=',$content);
     return $content;
}

Now that wasn’t too hard, now was it?

8 thoughts on “How to have normal YouTube-links handled by WP YouTube Lyte”

  1. Hi Frank,
    the functions.php hack seems to work in my test site (I use the Magazine Premium theme).
    However, I see the wp youtube lyte div “lyte-wrapper” in the home:
    nazioneindiana.bamaulion.net
    but not in a single post, e.g.:
    nazioneindiana.bamaulion.net/2013/video3/
    Is this normal?

    Reply
    • Hmmm, video3 is neither LYTE on HP or in single post. The posts that do have LYTE, both on HP and in single post, are video1, video7 and test video.
      I suppose the problem is that the YouTube URL in the other posts does not match the URL specified as replace-needle in the functions-hack (i.e. “http://www.youtube.com/watch?v=”). Can you copy/paste the exact URL you use in e.g. post video3?

      Reply
  2. Oh, sorry to bother you Frank, the site is a development site with a lot of cruft and I am an “https everywhere” (firefox plugin) user, so many urls were pasted https.
    Here is the list:
    test video http://www.youtube.com/watch?v=cxRyM4QlonM
    video8 https://www.youtube.com/watch?v=B3ue3IgKETU
    video7 httpv://www.youtube.com/watch?v=cv5WLLYo-fk (former test adding the “v”)
    video6 https://www.youtube.com/watch?v=3tv-JbAurcg
    video5 https://www.youtube.com/watch?v=lXQEQ7WMw2E
    video4 https://www.youtube.com/watch?v=-TzjPqaglbI
    video3 https://www.youtube.com/watch?v=-bAGaAKKVIk
    video2 https://www.youtube.com/watch?v=u88-Qy35oZM
    video1 http://www.youtube.com/watch?v=cxRyM4QlonM
    I must go right now and can’t modify the urls, read you later. Perhaps you could edit the str_replace to a regexp covering https as well?

    Reply
    • preg_replace (regexp) certainly is a possibility, but it is significantly slower than a normal str_replace, so best option is to standardize on one URL (i.e. always http or https) and work with str_replace (adjusting the needle if needed for ‘https’)?

      Reply
  3. Hello Frank,
    I added a second custom function for https:
    /** force wp youtube lyte on https://www.youtube.com url's as well */
    add_filter('the_content', 'force_lyte_parse_https', 1);
    function force_lyte_parse_https($content) {
    $content=str_replace('https://www.youtube.com/watch?v=','httpv://www.youtube.com/watch?v=',$content);
    return $content;
    }

    I modified the urls and tested some different use scenarios with WP Super Cache and Autoptimize, it seems to wok very well.

    Reply
    • oh, but if you do absolutely want the flexibility of parsing both http- and https-links, one preg_replace would be better then 2 str_replaces (in two separate functions). so here goes;
      /** force wp youtube lyte on http(s)://www.youtube.com url's as well */
      add_filter('the_content', 'force_lyte_parse', 1);
      function force_lyte_parse($content) {
      $content=preg_replace('/https?:\/\/www.youtube.com\/watch\?v=/','httpv://www.youtube.com/watch?v=',$content);
      return $content;
      }

      Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.