Redirect user to dedicated page based on user role while logged in (3 posts)

  1. Fanaticweb
    Posted 3 years ago #

    Hi everyone,
    I'm trying to get a user to be redirected from one single core page (domain.com/my_account) based on their user role.

    I am using the following plugins: Peter's login redirect, Members
    Although both plugins work in harmony, they have yet to complete this task.
    Live example:
    User A = Role defined to access the Account page
    User B = Role defined as a limited user therefore will be redirected to the warning page.

    So if user A logs in and attempts to access the My Account page, it works fine.

    If User B logs in, they are being redirected to the Warning page.

    So far so good for the Login process and redirection after logging in.

    But, if at some point, while the user is navigating the site and they are logged in as User B, if they click on the My Account link, they are redirected to the Homepage as it was set through the Peter's redirect plugin, this does the job half way as ultimately I would like them to be redirected to the Warning page.

    What I am trying to do is once the user is logged in, no matter where they are on the website, if they click on the My Account link, they will be redirected to their respective page based on their user role, not only after they login.

    Is there a way to achieve this behaviour? I'm not a coder but I can find my way around when it comes to inserting code in Functions or the core theme.

    Thank you for any assistance you may provide.

  2. bcworkz
    Posted 3 years ago #

    Needs some work, but maybe something like this added to your theme's functions.php (you should create a child theme to protect your theme hacks from theme upgrades):

    function warn_redirect() {
       global $post;
       if( 1234 == $post->ID && !current_user_can('edit_pages')) {
    add_action('template_redirect', 'warn_redirect');

    Change 1234 to the actual ID of the My_Account page, assuming it is a WP post type 'page' which has IDs just as regular posts do. Change 'edit_pages' to a capability that the warning user does NOT have but users that do not get the warning DO have. The Members plugin makes it easy to set capabilities up any way you like. Change '/warning-permalink' to the actual permalink or url to the warning page. Untested, so may need tweaking beyond the changes outlined. Good luck.

  3. Fanaticweb
    Posted 3 years ago #

    Thank you so much BCWorkz, I'll definitely give this a try and tweak it to match the content on my end.

Topic Closed

This topic has been closed to new replies.

About this Topic