• Resolved Song Simian

    (@song-simian)


    Hi, I love how much faster WP YouTube Lyte videos are than embedded Youtube links. Unfortunately, I have hundreds of embedded videos already on my site. Here’s a typical example of such an embedded code:

    <iframe src=”https://www.youtube.com/embed/EYdYTR_FAqM&#8221; width=”560″ height=”315″ frameborder=”0″ allowfullscreen=”allowfullscreen”></iframe>

    Of course, I could just use the Better Search Replace plugin to remove extraneous parts so this link could work with Wp Youtube Lyte (I just copy the URL for Lyte videos), like this:

    https://www.youtube.com/EYdYTR_FAqM

    This works (Lyte reads it well and creates a video on WordPress, even though the URL, if copied into the URL box, goes to a 404 on Youtube). However, since embedded Youtube links often have wildly different links, I have to be careful and it’s also a pain the posterior to double-check.

    Is there a way to change all standard Youtube embedded links to something that Wp Youtube Lyte could read to create videos? Or am I stuck with using Better Seach Replace plugin? Please help. Thanks!

    • This topic was modified 6 years, 5 months ago by Song Simian.
    • This topic was modified 6 years, 5 months ago by Song Simian.
    • This topic was modified 6 years, 5 months ago by Song Simian.
Viewing 13 replies - 1 through 13 (of 13 total)
  • Plugin Author Optimizing Matters

    (@optimizingmatters)

    I was going to write you’re stuck with better search replace @song-simian, but then I thought what the heck and came up with this code snippet;

    add_filter('lyte_content_preparse','iframe_catcher');
    function iframe_catcher($in) {
      preg_match_all('#<iframe src="https:\/\/www\.youtube\.com\/embed\/(.*)"(?:.*)><\/iframe>#Usm', $in, $matches, PREG_SET_ORDER);
      foreach ($matches as $match) {
    	$in = str_replace($match[0], 'httpv://youtu.be/'.$match[1], $in);
      }
      return $in;
    }
    Plugin Author Optimizing Matters

    (@optimizingmatters)

    slightly improved code snippet;

    add_filter('lyte_content_preparse','iframe_catcher');
    function iframe_catcher($in) {
      if (preg_match_all('#<iframe src="https:\/\/www\.youtube\.com\/embed\/(.*)"(?:.*)><\/iframe>#Usm', $in, $matches, PREG_SET_ORDER)) {
    	foreach ($matches as $match) {
    	  $in = str_replace($match[0], 'httpv://youtu.be/'.$match[1], $in);
    	}
      }
      return $in;
    }
    Thread Starter Song Simian

    (@song-simian)

    Wow, thanks! Where do I put this code, BTW? Sorry, I’m very computer illiterate. When I put it into my “Additional CSS” box in my theme’s customization option, I get a lot of red warning X’s (?):

    View post on imgur.com

    Plugin Author Optimizing Matters

    (@optimizingmatters)

    ouch, “additional CSS” will not work as this is PHP-code. install the “code snippets”-plugin and add this as a new snippet (active on front-end only).

    Thread Starter Song Simian

    (@song-simian)

    OK, thanks. I’ll do this. One question, though: is there a way to add this code into a file without resorting to a plugin (I like to keep my websites simple). I mean, add the code to a particular file (accessible by downloading/uploading from Filezilla maybe)? OK, please let me know. Thanks a ton!

    Plugin Author Optimizing Matters

    (@optimizingmatters)

    you could add it to your (child) theme’s functions.php, but it would get overwritten there when you update the theme. there is also a slightly higher risk of your site breaking when adding it there instead of using code snippets, which is why in general I advise people to use “code snippets” ??

    Thread Starter Song Simian

    (@song-simian)

    Thanks for the recommendations. I’ve followed the instructions. Added the Code Snippets plugin and put in your (second) code. It does improve the speed a bit. However, it’s still considerably slower than if I just remove the Youtube embedded codes themselves.

    For example, on this URL: https://www.songsimian.com/best-double-bass-pedal-reviews-setup-cheap/

    Original GTMetrix page speed with Youtube embedded links:

    View post on imgur.com

    Once I added the Code Snippets and put in your code:

    View post on imgur.com

    Deactivated Code Snippets plugin but removed all embedded codes till only Youtube URL remains:

    View post on imgur.com

    So while fast, it’s still not as fast as if I Better Search Replace the embedded codes by removing extraneous details.

    Maybe the code is superseding some embedded codes but not others? Can it be expanded? I’ve included a text file of all the variations that I’ve found in my embedded Youtube links. Would this help?

    https://drive.google.com/file/d/105B328TawtW-fCwd7Bn0HP03pukhN0uI/view

    Plugin Author Optimizing Matters

    (@optimizingmatters)

    Maybe the code is superseding some embedded codes but not others?

    probably ??

    Can it be expanded?

    undoubtedly ??

    I’ve included a text file of all the variations that I’ve found in my embedded Youtube links. Would this help?

    it would. I’ll see if I can find some time for this on of these days ??

    Thread Starter Song Simian

    (@song-simian)

    Thanks! This would maybe help your plugin too since, I’d imagine, tons of people also have old Youtube embedded links they’d need to become Lyte-readable links. ??

    Plugin Author Optimizing Matters

    (@optimizingmatters)

    here you go;

    
    add_filter('lyte_content_preparse','iframe_catcher');
    function iframe_catcher($in) {
      if (preg_match_all('#<iframe(?:.*)?\ssrc=["|\']https:\/\/www\.youtube\.com\/embed\/(.*)["|\']\s(?:.*)><\/iframe>#Usm', $in, $matches, PREG_SET_ORDER)) {
    	foreach ($matches as $match) {
    	  $in = str_replace($match[0], 'httpv://youtu.be/'.$match[1], $in);
    	}
      }
      return $in;
    }

    this should work on all of your iframed youtubes ??

    Thread Starter Song Simian

    (@song-simian)

    Wow, this works great! I’ll test it a bit more, but it seems to convert all my Youtube videos to Lyte videos. Thanks a ton!

    Plugin Author Optimizing Matters

    (@optimizingmatters)

    super! I might even add this to one of the next updates ??

    Plugin Author Optimizing Matters

    (@optimizingmatters)

    this indeed went into 1.7.5 which I’m pushing out as I write ??

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘How to replace Youtube embedded links with Lyte URL’s’ is closed to new replies.