I’ve been behind on announcing new projects, but I wanted to make sure I shared this one: WP404 is a framework for capturing additional information and details about WordPress 404 errors, packaged as a WordPress plugin.
The plugin was born out of a need to capture tricky, time-based 404s on a client site. I figured I could either throw something together quickly on the client’s dime or spend my lunchtime and evening building something the community could use. Guess who didn’t want a half-assed tool??
At its core, WP404 is a library of “reporters”, callbacks hooked onto the “wp404_report_data” filter. Each reporter is passed the current $report
array, as well as the current WP_Query
object. At the end, the $report
array is sent to the PHP error log for diagnostics.
Building custom WP404 reporters
There are a handful of reporters built-in, but the power of WP404 is that it lets you write whatever reporters you might need:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/** * Save the IP address of a user when they hit a 404 page. * * @param array $report The data that has been compiled for * this 404 error. * @param WP_Query $wp_query The WP_Query object that determined * the 404 status. */ function wp404_save_user_ip( $report, $wp_query ) { $report['ip_address'] = $_SERVER['REMOTE_ADDR']; return $report; } add_filter( 'wp404_report_data', 'wp404_save_user_ip', 10, 2 ); |
Obviously the more you’re logging, the bigger your logs will be, so be conservative in how you use WP404. It’s not meant to be a “pedal to the metal” plugin all the time, but when you’re trying to track down mysterious 404 errors it can be a tremendous help.
Contributing to WP404
Naturally, WP404 is available on GitHub under the MIT License, and contributions via pull request are more than welcome. There are no plans at this point to submit the plugin to WordPress.org (it uses PHP namespaces, which aren’t supported by servers running old versions of PHP, though if you’re still on PHP 5.2 stop what you’re doing and upgrade/move your site!), but it should run swimmingly on servers running PHP 5.3 or higher.
Leave a Reply