Skip to content

Instantly share code, notes, and snippets.

@DrizzlyOwl
Forked from anonymous/functions.php
Created December 7, 2015 11:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DrizzlyOwl/819a417fb1cf8504bce4 to your computer and use it in GitHub Desktop.
Save DrizzlyOwl/819a417fb1cf8504bce4 to your computer and use it in GitHub Desktop.
Safely force WordPress to 404
<?php
// safe 404
// Force WordPress to 404 on the current template
// SEO Friendly!
// =================================================
function safe_404(){
global $wp_query;
$wp_query->set_404();
status_header(404);
nocache_headers();
get_template_part('404');
exit;
}
<?php
/**
* Located in functions.php
*
* This function will:
* - Interrupt the current WP_Query global.
* - Force WordPress to include the 404 template automatically.
* - Declare a 404 Not Found HTTP code.
*
*/
safe_404();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment