Best way to inspect dead links using your 404 page

404

I searched for some instructions on how to easily find dead links on my blog. I found some plugins that do just that but instead of using a plugin to do it why not use this cool method that can also provide you with a custom 404 page ?

The WordPress Codex directory is way cool and it is maybe one of the best wordpress resources out there. Here’s the link that interests us in particular: Creating an Error 404 Page. You just need to create a 404.php file in your theme directory (in case you don’t already have one). Put the following code in it and customize aiding yourself with the comments:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php get_header(); ?>
   <div id="post-entry">
     <h1>Whoops !</h1>
	     <div class="post-content">
	     	<p>You 
				<?php
				#some variables for the script to use
				#if you have some reason to change these, do.  but wordpress can handle it
				$adminemail = get_bloginfo('admin_email'); #the administrator email address, according to wordpress
				$website = get_bloginfo('url'); #gets your blog's url from wordpress
				$websitename = get_bloginfo('name'); #sets the blog's name, according to wordpress
				
				  if (!isset($_SERVER['HTTP_REFERER'])) {
				    #politely blames the user for all the problems they caused
				        echo "tried going to "; #starts assembling an output paragraph
					$casemessage = "All is not lost!";
				  } elseif (isset($_SERVER['HTTP_REFERER'])) {
				    #this will help the user find what they want, and email me of a bad link
					echo "clicked a link to"; #now the message says You clicked a link to...
				        #setup a message to be sent to me
					$failuremess = "A user tried to go to $website"
				        .$_SERVER['REQUEST_URI']." and received a 404 (page not found) error. ";
					$failuremess .= "It wasn't their fault, so try fixing it.  
				        They came from ".$_SERVER['HTTP_REFERER'];
					mail($adminemail, "Bad Link To ".$_SERVER['REQUEST_URI'],
				        $failuremess, "From: $websitename <noreply@$website>"); #email you about problem
					$casemessage = "An administrator has been emailed 
				        about this problem, too.";#set a friendly message
				  }
				  echo " ".$website.$_SERVER['REQUEST_URI']; ?> 
				and it doesn't exist. <?php echo $casemessage; ?>  You can click back 
				and try again or try using the search box in the sidebar.
			</p>
	     </div>
	</div>
 
 
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Easy enough ? I think it is an awesome method. At least for testing if not for permanent use. Hope this helps. Oh, and by the way, if you have a large blog be ready to be bombarded with emails generated from this 404 page.

Social bookmarking:
  • Digg
  • del.icio.us
  • Google Bookmarks
  • StumbleUpon
  • Technorati
  • Mixx
  • Sphinn
  • Reddit
  • Live
  • Twitter
  • Yahoo! Bookmarks

This entry was posted in php, scripts, tips and tricks, wordpress and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
  • Dan
    I'm glad I could help. A thumbs-up would be greatly appreciated. Thank you.
  • Thank you for snippet, implemented at my blog (simplified to exclude output to user and only left mail part).

    Got first alert on one of plugins trying to lookup icon which wasn't there. Hardly critical but proves system works. :)
blog comments powered by Disqus