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:

if ( class_exists( 'Clean_404_Email' ) )
	new Clean_404_Email;

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!

function new_subcategory_hierarchy() {	
	$category = get_queried_object();

	$parent_id = $category->category_parent;

	$templates = array();
	if ( $parent_id == 0 ) {
		// Use default values from get_category_template()
		$templates[] = "category-{$category->slug}.php";
		$templates[] = "category-{$category->term_id}.php";
		$templates[] = 'category.php';		
	} else {
		// Create replacement $templates array
		$parent = get_category( $parent_id );

		// Current first
		$templates[] = "category-{$category->slug}.php";
		$templates[] = "category-{$category->term_id}.php";

		// Parent second
		$templates[] = "category-{$parent->slug}.php";
		$templates[] = "category-{$parent->term_id}.php";
		$templates[] = 'category.php';	
	return locate_template( $templates );

add_filter( 'category_template', 'new_subcategory_hierarchy' );

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:

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