Return 410 Gone for Removed Pages
Answers requests for pages you deleted on purpose with a "410 Gone" status instead of a 404, so search engines drop them from results faster.
Opens WPIDE on
Code
// Addresses you removed on purpose and that will not come back. Empty = off. A trailing * matches everything below. Examples:
// '/old-promo/',
// '/discontinued-products/*',
$wpide_gone_paths = array();
add_action( 'template_redirect', function () use ( $wpide_gone_paths ) {
// Only for addresses that are missing anyway, so live content can never be hidden by mistake.
if ( ! $wpide_gone_paths || ! is_404() ) {
return;
}
$path = isset( $_SERVER['REQUEST_URI'] ) ? strtolower( (string) wp_parse_url( wp_unslash( $_SERVER['REQUEST_URI'] ), PHP_URL_PATH ) ) : '';
foreach ( $wpide_gone_paths as $gone ) {
$gone = strtolower( (string) wp_parse_url( home_url( $gone ), PHP_URL_PATH ) );
$match = '*' === substr( $gone, -1 )
? 0 === strpos( $path, rtrim( $gone, '*' ) )
: untrailingslashit( $gone ) === untrailingslashit( $path );
if ( $match ) {
// 410 tells search engines the page is gone for good, so they drop it faster than a 404.
status_header( 410 );
nocache_headers();
header( 'X-Robots-Tag: noindex' );
return;
}
}
} );
Notes
The list is empty by default; only addresses that are already missing get the 410, so live content is never hidden.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.