Skip to content

Instantly share code, notes, and snippets.

@gyrus
Created August 12, 2012 16:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save gyrus/3332597 to your computer and use it in GitHub Desktop.
Save gyrus/3332597 to your computer and use it in GitHub Desktop.
Override a WordPress 404 with a specific template
<?php
/**
* Override a 404
*
* @param string $template If passed, this template will be included and
* the request will be terminated if the inclusion
* was successful
* @param string $type If not a 404, what type of request should this be
* flagged as? Must correspond to one of the
* @return void
*/
function pilau_override_404( $template = null, $type = 'page' ) {
global $wp_query;
// Change type of query
$wp_query->is_404 = false;
$type_property = "is_$type";
if ( property_exists( $wp_query, $type_property ) )
$wp_query->$type_property = true;
// Make sure HTTP status is good
status_header( '200' );
// Load template?
if ( $template ) {
$template_found = locate_template( $template, true );
if ( $template_found )
exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment