How To Check Whether an External URL Exists with WordPress’ HTTP API

As an avid WordPress developer (hopefully as I am), you might run into a scenario where you’ll have to check whether an external URL exists or not (Meaning, if the URL responds with a 404 status code or a 202, which means the page exists – in most cases). For instance, before attempting to download a file, you might want to make sure it actually exists, and that the target URL is not leading to a 404 page.

Another use case scenario might be if you want to verify a URL when a user posts a new comment. This can actually help if your site is being regularly targeted by spammers (happens to the best of us). This was my case.

Obviously, that URL does not exist, unless you go ahead and buy that domain right now!

Step-by-Step Walk-through

So here’s a short description, step by step, of how this thing works:

Quick note: When dealing with HTTP requests in WordPress you should always, always use WordPress’ built-in WP_HTTP API. And it has much more to it than just best practices.

  1. We use the pre_comment_author_url filter that executes when a user submits a new comment. The URL is passed as a variable ($comment_author_url).
  2. We make sure we’re not on the admin side of WordPress, so in case an administrator decides, against any common sense, to assign a  broken URL to a comment, they will be able to do it. We also make sure the URL field is not empty before continuing.
  3. At this point, we initiate a HTTP HEAD request to the target URL. The reason I chose to perform a HEAD request as opposed to a regular GET request is simply because a GET request will also serve the HTML code, which we don’t really need in our case. Theoretically speaking, we should have enough information in the response headers to determine weather the URL exists or not.
  4. We define an array, $accepted_status_codes, which stores HTTP status codes that we consider as accepted.
  5. Now we make sure the HTTP request was successful and that the response code matches one of the status codes we defined earlier. If all is positive, it means that the URL exists, or at least responded with one of the status codes we accepted, so now we can return the URL.
  6. If the request has failed, or if we got a status code that didn’t match, whether it is a 404 or 500, we return an empty string, since we don’t want to show a URL that does not exist.

The Actual Snippet

Here’s what makes it all possible.

Please note that this code is not bulletproof, and will not work in all scenarios. For an example, there are severs configured in a way that if a nonexistent URL is being requested, a 301 response code will be issued, referring to the site’s homepage. And I’ve seen that happen. So, in case you’re interested in using this snippet, feel free to use it, but it’s your own responsibility to test the code on your WordPress install.

I have tested this code on my development server and it seems to be working just fine. If for some reason it doesn’t work for you, or works differently than expected, please let me know in the comments. Also if you have any questions about this piece of code, lay them down on me!

Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">