Broken YouTube Checker now with oEmbed!

Ryuzaki

お前はもう死んでいる
Moderator
BuSo Pro
Digital Strategist
Joined
Sep 3, 2014
Messages
6,140
Likes
12,859
Degree
9
I've been preparing a site, getting it ready to bring on some writers and start pumping out content at scale. During all of this, I found a dead YouTube video that wasn't being reported by my link checker. So I tried another and another...

NONE OF THEM WORKED!

The problem? Nobody has bothered to make sure to use the YouTube oEmbed API since Wordpress dropped the shortcodes in favor of oEmbed (where you paste the link and voilà, it works!)

The solution? I made a thing.

Note: This works for youtube.com, not youtu.be... you'll want to manually replace those or add to the code.​

I hate plugins. More than anything else when they start bloating your database saving ridiculous stuff that has no business being there. So those were my main demands:
  1. It has to check oEmbeds
  2. It has to print to screen
That's it. Simple and easy. You run the script, it tells you if any posts have broken YouTube videos, you click the post title to open a new tab, you find a replacement and fix it.

Here's how it looks:
pbh8G6m.png

It's numbered by broken video count, not by post. If a post has more than one problematic video it will list that post as many times as needed.

Of course, if you have no deleted or missing videos, private and unlisted videos, or whatever else, you'll see:
eqjESHr.png

Now, I need to explain how it's used so anyone not in the know can follow the directions. I tried to keep it as simple as possible. It's mainly PHP with a tiny splash of Regex.

PHP:
<?php
/*
 * Template Name: YouTube oEmbed API Checker
 * Description: Searches All Posts for YouTube oEmbeds and Checks Them Against the API
 */
get_header(); ?>

<div class="container">
  <div class="row">
    <div class="column">
    <h2>YouTube Checker</h2>
      <ol>
 
<?php
$allGood = 0;
function ryu_all_vids() {
  $args = array(
      's' => 'youtube.com/watch?v=',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'orderby' => 'date'
  );
  $postlist = get_posts($args);
  foreach($postlist as $post) {
    preg_match_all('|youtube.com/watch\?v=([a-zA-Z0-9_-]+)|', $post->post_content, $videoIDs);
    for ($counter = 0; $counter < sizeof($videoIDs[1]); $counter++) {
      $videoID = $videoIDs[1][$counter];
      if (ryu_yt_api($videoID)) {
        // do nothing
      } else {
        echo "<li><a target='_blank' href='" . get_permalink($post) . "'>" . get_the_title($post) . "</a><br /><div style='margin-left:40px;'>";
        echo $videoIDs[1][$counter] . " - <span style='color:#FF0000;'>Dead :(</span></div></li><hr>";
        global $allGood;
        $allGood++;
      }
    }
  }
}
ryu_all_vids();

function ryu_yt_api($videoID) {
    $theURL = "http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=$videoID&format=json";
    $headers = get_headers($theURL);
    if (substr($headers[0], 9, 3) == "404" || substr($headers[0], 9, 3) == "401") {
        return false;
    } else {
        return true;
    }
}

if ($allGood == 0) { echo "You have no broken videos to fix!"; }
?>
      </ol>
    </div>
  </div>
</div>
 
<?php get_footer(); ?>

The first thing that should be obvious is that I put this in a page template. You'll want to save this as something like:

youtube-check.php
And then upload it with the rest of the page templates on the server inside of your theme folder.

If you want it to load in your site like normal, I've included the header and footers, but you'll have to figure out your own unique div's and classes to use. Or strip them out and go straight white screen.

You will then need to create a new page and name it something sweet like YouTube oEmbed Checker. That's it, just so you can find it. Make sure you give it a reasonable slug that you can type in like /youtube-checker/.

Now, this sucker auto-fires and if you have a lot of posts it's going to eat up some resources. What you want to do is some combo of the following:
  • Make sure it's no-index. Never link to it either.
  • Block all robots from accessing it in robots.txt
  • Better yet, move it to the Trash when not using it.
  • Make sure it's not in your Sitemap!
  • Use it once a month maybe.
  • Even use .htaccess to block access to it accept from your home IP address.
  • Set the page to Private in Wordpress for admins only.
The point is, you don't want anyone or anything accessing this thing slowing down your site and eating up resources. This is critical for sites big and small.

You can then browse to it at domain(com)/youtube-checker and see what's crack-a-lackin'.

As far as the code goes, it's searching all of your published posts, pulling out the content, pulling out YouTube URLs, yanking the v? parameter, and pushing each one through the oEmbed API. It only reports back the broken ones.

I hope someone finds this helpful. I'm sure many people have found the same issue with the current checkers on the market.

What It Could Use...
And maybe one of you crafty bastards can figure out a good way to not use that global variable and let us know. I went brain dead by the end and couldn't wrap my head around a better option without a large rewrite.​

If this saves you a million man hours then consider picking up a BuSo Pro account upgrade to say thanks!
 
Back