The Robservatory

Robservations on everything…

 

A simple URL tester for WordPress

One of the issues with blogs is that, over time, links embedded in posts can break. Sometimes they break in graceful ways (redirecting to an acquiring company's site), sometimes in not so graceful ways ("Site not found!"), and sometimes in downright horrid ways (a porn spammer takes over a URL).

I wanted a way to test any URL in entries I've posted here, so a buddy wrote the basics of a tool to query the database and extract URLs from the posts. I took his core, then did some digging on the web, and mangled together a simple PHP app that will scan all your blog posts for URLs, and test to make sure each one still connects.

The results are displayed in an ugly-but-usable table form:

The first column is the URL being tested, and the second column displays the post numbers where that URL can be found. Any highlighted rows reflect dead links; no highlighting means that the URL opened as expected. Read on for the code and a basic how-to…

Here's the code; use the toolbar buttons to copy to the clipboard, paste into a new PHP document, and save at the top level (i.e. where you find wp-config.php) of your WordPress installation.

Then just enter the URL of the new file in your browser, and wait. You'll see a table similar to the above get built, one line at a time. For my 325ish entry blog, it takes about five minutes to run through the script.

Before using this script, be aware of the following:

  • This is an all-in script; it checks all your blog posts for URLs. If you have a huge number of posts, it will take a very long time to run.
  • It's not perfect; some valid hosts (amazon.com for one) come up as offline. It's easy to skip these in the output, as I know they're valid.
  • The program can't check to see that a link goes where you expect it to go, so it doesn't address the situation where a spammer has taken over an old domain. However, all URLs are clickable, so you can click those that seem questionable to see where they go.
  • The post number links will open edit windows to the associated blog post, making it easier to update the offending URLs.
  • The script expects to be at the top level of your WordPress installation (at the same level as wp-config.php). It will also fail if you don't use the "wp_" table prefix—if you use a different prefix, edit line 12 to reflect the proper value.
  • A warranty is not included with this free bit of poorly-written code. If you want one, they're available for $1,000,000 each, and are good for 60 seconds.

I'm sure this can be written in a much better way, but for my needs, it was perfect. I found six dead links, and fixed them all within a few minutes.

2 Comments

    1. You need to be able to ssh in and install that PHP script, so probably not. However, if you can use WordPress plug-ins, there are tons that do similar stuff; here's a quick search I ran, though I haven't tried any myself.

      Why did I write my own instead of using a plug-in? Partly as a learning exercise, partly because I wanted something really simple and light.

      -rob.

Comments are closed.

The Robservatory © 2020 • Privacy Policy Built from the Frontier theme