Warm tip: This article is reproduced from stackoverflow.com, please click
html php redirect wordpress

Redirect WordPress User based on user role when accessing a page

发布于 2020-04-13 10:02:47

I'm trying to create a simple redirect when someone tries accessing a certain page depending on user role.

I've created a site with membership functionality that offers certain things to paid members. Free members can only access one page after login but if they know the URL to the paid area they can see the content. I was wondering if there's a way to create a 301 redirect so that when the free member tries to go on the paid page, it redirects them back to the free area.

Is this possible? If you need more info, please ask!

Thanks David

Questioner
David Morris
Viewed
44
Ravi 2020-02-03 18:31

You can also try like this and make it more wordpress friendly.

function role_redirections() {
$loggedin_user = wp_get_current_user();  
if (!is_user_logged_in() || !in_array( 'memorial_user', (array) $loggedin_user->roles )) {
    $location1 =  get_option( 'siteurl' ) .'/member-portal/';
    header('Location: '.$location1.'');
    } 
}
add_action('wp_head', 'role_redirections');