Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.