WordPress overide 404 for private pages

I was wondering how would I go about overiding wordpress’s default 404 on private pages when not logged in? Had a look around at post-template.php however nothing I could change that made it work?

Related posts

Leave a Reply

2 comments

  1. to create custom message for private pages

    add this code to the top of your 404.php in your theme:

    //Redirect if requested page is private
    $page_requested = get_page_by_path($_SERVER['REQUEST_URI']);
    
    $page_status = get_post_status( $page_requested->ID );
    if( $page_status == 'private' ){
        wp_redirect( get_site_url().'/path/to/private/login' ); exit;
    }
    
  2. I’m not 100% sure I understand what you’re asking, but it sounds like you want to create a custom 404 page and change that depending on whether someone is logged in or not.

    To do that: create a 404.php file and upload it to your theme directory (the same theme directory with the header.php, footer.php, sidebar.php, etc). Then, in the code, use this function:

    if( is_user_logged_in() ) {
        // Code for logged in users
    }
    else {
        // Code for unknown users
    }
    

    That should do it it for you.

    Sources: Creating an Error 404 Page, Function – Is user logged in