• hello,

    Without using a plugin, looking to only do the following:

    0) a public page does not check for users.
    1) make a page force a user to be logged in (ie. a restricted access page)
    2) if a visitor goes to this page, wordpress redirects them to the custom login page
    3) allow admin to view any page (if they are logged in)

    currently using:
    add_filter( ‘login_redirect’, ‘my_login_redirect’, 10, 3 );

    and then putting PHP code on pages where it checks the visitor or user type (admin or just regular user).

    All my variations in code either do not work, creates a login loop, or does not do all three requirements.

    so I have 3 types of pages:
    – all public
    – Only user or admin
    – only admin

    this can’t be that hard, but I have not got it correct yet. Does anyone have a page that explains what I should be doing? The WP Core documentation takes me into many different directions… I get a little lost looking at it ??

    thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi @sayze35 ,

    function custom_redirects() {
        if ( is_page('contact') && !is_user_logged_in()) {
            wp_redirect( home_url( '/new-contact/' ) ); // add login your you wan't to redorect
            die;
        }
    }
    add_action( 'template_redirect', 'custom_redirects' );

    for more you can visit this url https://developer.www.ads-software.com/reference/hooks/template_redirect/

    This hook may be helps you.

    Thanks.

    Thread Starter sayze35

    (@sayze35)

    Chintesh Prajapati, 2 words

    THANK YOU!

    ?? yes! simple. and it just works…

    I appreciate you pointing me in the right direction.

    Hi @sayze35 ,

    You are welcome, Can you please mark this topic to resolved?

    Thank you.

    Thread Starter sayze35

    (@sayze35)

    So I had over looked something, it keeps the logged in user ON the login page, EVEN though on add_filter( ‘login_redirect’, ‘123_login_redirect’, 10, 3). – it has them going to another page?

    Something simple still must be incorrect, Why is a logged in user staying on the same login page? Nothing is telling it to do that AND login_redirect is saying go to whatever page the logged in user can see. BUT the login_redirect() hook is never being called????

    
    function custom_redirects() {
     
       $loggedin = is_user_logged_in();
       debug_to_console("in custom_redirects and are you logged in:".$loggedin);
    
        // 33 is restricted page
        if ( is_page(33) && !is_user_logged_in()) {
            wp_redirect( home_url( '/login/' ) ); 
            die;
        }
        // there are other pages that need either a logged in user or admin
    
    }
    
    add_filter( 'login_redirect', '123_login_redirect', 10, 3 );
    function 123_login_redirect( $redirect_to, $request, $user ) {
    
        global $current_user;
        debug_to_console("in 123_login_redirect");
    
        if ( !$user->ID > 0 ) {
    
            debug_to_console("- no USER -, trying global user");
            if (!$current_user->ID > 0) {
                debug_to_console("- no global USER -, trying to get user");
                $current_user = wp_get_current_user();
                if (!$current_user->ID > 0) {
                    debug_to_console("- no USER - must be loggout");
                    wp_redirect('/login/');
                    exit;
                }
            }
    
        } else {
            $current_user = $user;   // this is the passed in user
        }
    
        debug_to_console("cur id".$current_user->ID);
    
       // depending who is logging in
       // 25 is special user
        if ($current_user->ID ==25) {
            return "https://domain.com/go_to_special_page_url";
        } else if (current_user_can('administrator')) {
            return "https://domain.com/wp-admin/";
        }
        else if (!$current_user->ID > 0) {
    		$current_user = wp_get_current_user();
    		if (!$current_user->ID > 0) {
                        //  wp_redirect('/login/');
                        //  exit;
                       debug_to_console(" HERERERERERER");
    		} else {
                return "https://domain.com/this_page/";
            }
    	} else {
    		 return "https://domain.com/that_page/";
    	}
    }
    
    

    can you see what I’m doing wrong?

    • This reply was modified 4 years, 7 months ago by sayze35. Reason: added code tags
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘what are the simple hooks, filters, process to restrict page access’ is closed to new replies.