WordPress.org

Ready to get started?Download WordPress

Forums

Hide page for logged in Users (5 posts)

  1. mmarte85
    Member
    Posted 2 years ago #

    Hi guys,

    I wanna do the following:
    If a user is logged out, he can view the page id=10, if a user is logged in and view page id=10 he will be redirected to page id=5.

    I tried some codes in my theme's header, but nothing worked. Is there any solution (theme modification in header.php or functions.php - I don't want to use a plugin for this!) for this on?

    Thank you,
    Michael

  2. sfyfe
    Member
    Posted 2 years ago #

    Have you tried the User Access Manager plug-in?

  3. Big Bagel
    Member
    Posted 2 years ago #

    Something like this should work:

    if ( is_user_logged_in() && is_page( 10 ) ) {
        wp_redirect( get_permalink( 5 ) );
        exit;
    }

    Edit:
    To make sure it redirects before any headers are sent you would probably want to wrap it in a function and use a hook. I'd put it in functions.php.

    add_action( 'init', 'check_redirect_page' );
    
    function check_redirect_page() {
        if ( is_user_logged_in() && is_page( 10 ) ) {
            wp_redirect( get_permalink( 5 ) );
            exit;
        }
    }
  4. mmarte85
    Member
    Posted 2 years ago #

    Big Babel, you're a genius! This one works like a charm. Thank you very much :-)

  5. oshi
    Member
    Posted 1 year ago #

    Hey Big Bagel hoping you can help me out to,

    with mine would be if user is login hide page id 100

    I have two pages one is for login and the other one is for wrong password displaying the message.

    I manage to redirect the user once they login to they profile page, but the wrong password page is available if you type on your browser http://www.mydomainname.com/wrong-password

    it shows for users who are logged in or for guest. so am trying to hide this one page from logged on user and just make it available for guest

Topic Closed

This topic has been closed to new replies.

About this Topic