Support » Developing with WordPress » Forwarding pages

  • Resolved Davood Denavi

    (@binarywc)


    I would prefer to not use a plugin but put code in my functions file to do this.

    I am working on a custom site for a photographer who does one-on-one photography lessons and she wants to give students their own gallery pages but also have a page to show select photos (with permission) from some of her students.

    She ways the student gallery to be at /gallery/student-gallery/ and the page with photos to be at /gallery/.
    The second page will be for logged out users to get a feel for the pictures she and her students take!

    She would like for students who are always logged in to always be forwarded to their gallery when clicking gallery in the navigation. This is the code I came up with to accomplish this but it does not seem to be working…. where am I going wrong?

    function my_redirect_user_by_role($user_login, $user)
    {		
    	global $user;
    	echo $role_name      = $user->roles[0];	
    
    	// redirect for student
    	if ( 'student' === $role_name && is_page('gallery') ) {
    		wp_redirect('https://mysite.com/gallery/student-gallery/');				
    		exit;
    	} 
    }
    add_action('wp_login','my_redirect_user_by_role');

    Thank you in advance.

Viewing 1 replies (of 1 total)
  • After some further research I figured out that I needed to add is_user_logged_in() && to my code like this:

     function gallery_login_check() {
    	 
        $current_user   = wp_get_current_user();
        $role_name      = $current_user->roles[0];
    	
        if ( is_user_logged_in() && 'student' === $role_name && is_page(54) ) {
            wp_redirect('/gallery/student/');
            exit;
        }
    }
    
    add_action('wp', 'gallery_login_check');

    I also changed the function name for clarity when reading the code later on! 🙂

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.