Cray
Cray

Reputation: 5493

WordPress: Redirect homepage and pages to account page when not in a specific user role

I want to redirect every page (including the homepage) in WordPress to a specific page. But I also want to set some exceptions. Pages I don't want to redirect. The redirect should only take place if the current user is not part of the user role "editor".

As example:

I want to redirect every user without that role to the page with the page ID 9. But the pages with the IDs 1,2,3 and 4 should not be redirected.

I managed to redirect the home page to the page with the ID 9. But only the home page.

But how can I add every other page to that funtion and also add the exceptions? I guess I have to white list the exceptions in a way but I just don't get it at the moment.

EDIT: Here's some more background: I have a user role (in this example "editor") with an own member area. Now I want to limit the access to the whole site for every user and role. But if the user is logged in with the role "editor", I want to allow them to navigate in the member area. But if the user isn't logged in, I need the login page and a register page to be open for everyone. Everything else should be redirected to the login page (ID 9).

This is the current code:

function redirect_homepage() {
    if( ! is_home() && ! is_front_page() )
        return;

    wp_redirect( get_permalink('9') );
    exit;
}

add_action( 'template_redirect', 'redirect_homepage' );

Upvotes: 0

Views: 749

Answers (1)

rank
rank

Reputation: 2544

The way I got your problem with your edit:

  1. You want to have your whole page only be accessible for logged in users.
  2. But there should be visible register and login pages, so users can register and log in.
  3. These pages are custom pages (not wp-admin) and you have the IDs of the pages.
  4. Only logged in user with the role 'editor' can use the whole page. If you are logged in, but do not have the role of 'editor' your access on the page is the same as for not logged in users (no access / so redirect to login page or other).

So you can put the code the functions.php file of your theme:

<?php

function editors_only() {

    /* Redirect not logged in users */

    // instead of '12' and '13' you write the page ids of your login and register pages
    if ( !is_user_logged_in() && !is_page( 12 ) && !is_page( 13 ) ) {
        wp_redirect( get_permalink(12) ); // redirect to login page
        exit;
    }

    /* Redirect logged in users */

    if ( is_user_logged_in() ) {

        $user = wp_get_current_user(); // get current user
        $user_access = false; // set boolean for access to false
        if ( in_array( 'editor', (array) $user->roles ) ) {
            $user_access = true; // if user role correct set boolean to true
        }

        if ( $user_access == false ) { // is user has not the editor role
            wp_redirect( get_permalink(12) ); // redirect to login page
            exit;
        }
    }
}
add_action( 'wp', 'editors_only' ); 

?>

I think it is not the best practice or intended to redirect your logged in users to your login page, as they are already logged in, but just do not have a "editor" role. Maybe you have an upgrade account page or other pages for this role of users. You can add this page id in your "redirect not logged in users" code (another && !is_page(id)) and put the ID of this page in the get_permalink(id) function if the user access is false.

Upvotes: 1

Related Questions