• rcrem

    (@rcrem)


    Hello,

    I currently have a template file that redirects users to separate pages based on whether or not they’re logged in. It is shown below. This is so I can show different versions of the home page to registered and non registered users. I need to create (or modify) a template file to redirect based on user role.

    For example, susbscribers who visit redirectpage are redirected to page-a, and editors are redirected to page-b. Is this possible?

    Thank you!

    <?php
    ob_start();?>
    /*
    Template Name: redirecT Courses
    */
    ?>
    
    <?php get_header(); ?>
    
    <?php if (is_user_logged_in()) {
    wp_redirect('courses-3');
    
    } else {
    wp_redirect('courses');
    } ?>
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
     ob_end_flush();>
Viewing 2 replies - 1 through 2 (of 2 total)
  • Jack Reichert

    (@jackreichert)

    In php you can’t redirect once the headers have been sent.

    So you should move your condition above the get_header() call.

    You also really shouldn’t use ob_start() here. You can read more about it https://php.net/ob_start

    Cheers!

    vrazer

    (@vrazer)

    You’re going to need to write up some code to detect the user role of the logged in user. It will probably go inside of the current is_user_logged_in blog that you have now.

    The code described here is not too bad.
    https://wordpress.stackexchange.com/questions/58916/how-to-get-role-of-user

    You can’t copy it verbatim, of course, but the function they wrote up will help you determine the role of the logged in user (you’d just need to retrieve the user ID of the logged in user). From there, you should be able to code up either an if/then or switch block in PHP that would let you redirect people.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Create template file that redirects based on user role’ is closed to new replies.