Get email alerts about 404 errors on your WordPress site

Ever wanted to get email alerts about 404 errors on your WordPress site?

Jeff Starr over at WP Mix posted a snippet yesterday for doing just that. And I, having a little time on my hands, decided to give it the ol’ OOP-once-over.

The original WP Mix post called for dropping the entire snippet into the top of your WordPress theme’s 404.php file, but I generally don’t like to clutter up my template files with extra non-theme stuff. So I converted it into a class which can then be easily instantiated in with these two lines:

<br />
if ( class_exists( 'Clean_404_Email' ) )<br />
	new Clean_404_Email;<br />

Not bad, considering the original snippet called for dropping in 83 lines of extra code!

I also converted the email format to a use a table, just so it’s a little more orderly about it.

Many users take advantage of freely-available tools like Google’s Webmaster Tools or other services to track a site’s 404 errors. This class gives you the short and sweet of it, and there’s no waiting around. If somebody gets a 404 on your site, WordPress will email you on the spot.

The gist is available on GitHub and also embedded below.

Photo used under CC. Photo by Jeremy Keith (adactio/Flickr)

Force sub-categories to use the parent category template

A couple of times in the last several years, I’ve needed sub-categories to inherit their parent’s archive template, but it’s just not something the Template Hierarchy supports. I’ve seen several plugins that tried and failed to do it, so finally I wrote a little filter that in my testing, works any number of levels deep, from sub-sub-categories to sub-sub-sub-categories. Enjoy!

<p>function new_subcategory_hierarchy() {<br />
	$category = get_queried_object();</p>
<p>	$parent_id = $category-&gt;category_parent;</p>
<p>	$templates = array();</p>
<p>	if ( $parent_id == 0 ) {<br />
		// Use default values from get_category_template()<br />
		$templates[] = &quot;category-{$category-&gt;slug}.php&quot;;<br />
		$templates[] = &quot;category-{$category-&gt;term_id}.php&quot;;<br />
		$templates[] = 'category.php';<br />
	} else {<br />
		// Create replacement $templates array<br />
		$parent = get_category( $parent_id );</p>
<p>		// Current first<br />
		$templates[] = &quot;category-{$category-&gt;slug}.php&quot;;<br />
		$templates[] = &quot;category-{$category-&gt;term_id}.php&quot;;</p>
<p>		// Parent second<br />
		$templates[] = &quot;category-{$parent-&gt;slug}.php&quot;;<br />
		$templates[] = &quot;category-{$parent-&gt;term_id}.php&quot;;<br />
		$templates[] = 'category.php';<br />
	}<br />
	return locate_template( $templates );<br />
<p>add_filter( 'category_template', 'new_subcategory_hierarchy' );</p>

Add ‘Edit User’ Toolbar link on author archives

Currently it takes 3-4 steps to get to a user’s edit screen from the front-end, which really comes down to a lot of wasted time when you’re working on a user-heavy site. One of my biggest usability pet peeves is unnecessary extra steps. On the WordPress front-end, we have ‘Edit *’ Toolbar links for objects like taxonomy terms and post types, so why not users?

The question was raised recently by John Blackbourn on Trac and I think it has a lot of merit. I’ll concede that the inherent behavior of an edit link on an archive shouldn’t be to edit an object but an author archive (usually) serves dual purposes: Author info and author archive.

Here’s the snippet I worked up from @lessbloat’s revised patch:

<br />
function ww_toolbar_edit_user_link( $wp_admin_bar ) {<br />
	$current = get_queried_object();</p>
<p>	// Check that it's a WP_User object and user is editable<br />
	if ( is_a( $current, 'WP_User' )<br />
		&amp;&amp; current_user_can( 'edit_user', $current-&gt;ID ) ) {</p>
<p>		// Add the menu<br />
		$wp_admin_bar-&gt;add_menu( array(<br />
			'id' =&gt; 'edit',<br />
			'title' =&gt; __( 'Edit User' ),<br />
			'href' =&gt; get_edit_user_link( $current-&gt;ID ),<br />
			'meta' =&gt; array(<br />
				'title' =&gt; __( 'Edit User' )<br />
			)<br />
		) );<br />
	}<br />
}<br />
add_action( 'admin_bar_menu', 'ww_toolbar_edit_user_link', 81 );<br />