Created
August 12, 2012 16:19
-
-
Save gyrus/3332597 to your computer and use it in GitHub Desktop.
Override a WordPress 404 with a specific template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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