Is there a way to avoid 404 pages in WordPress?

On my site, people can link to non-existing posts, which is fine because that’s the whole structure on my site… The problem is that you get 404 errors, which is not good for SEO. Is there a way in WordPress to avoid 404 pages? So if a post not exists, that you can do else instead of sending to Google that it’s a 404 page?

Related posts

2 comments

  1. One way to do this is to use the status_header filter. Adding the following to the functions.php file or your theme (or an appropriate plugin file) would do the trick:

    add_filter( 'status_header', 'your_status_header_function', 10, 2 );
    
    /**
     * Substitutes a 202 Accepted header for 404s.
     *
     * @param string $status_header The complete status header string
     * @param string $header HTTP status code
     * @return string $status_header
     */
    function your_status_header_function( $status_header, $header ) {
    
        // if a 404, convert to 202
        if ( (int) $header == 404 )
            return status_header( 202 );
    
        // otherwise, return the unchanged header
        return $status_header;
    }
    

    Alternately, you could add @header( $_SERVER[ 'SERVER_PROTOCOL' ] . ' 202 Accepted', true, 202 ); to your 404.php file before the get_header() call.

    The downside to this is that all 404s will be returned as 202, including legitimate Files Not Found.

    Users will still be served the 404.php template, so add your create post form there and you should be good.

Comments are closed.