0

I have a wordpress that users can register and become a member, and how do I get my homepage https://sample.com redirected to the settings page https://sample.com/setting for example. so when the user logs in, they cannot access the homepage which is https://sample.com

0

If you want to redirect the user after login use the below code:

/**
 * WordPress function for redirecting users on login based on user role
 */
function wpdocs_my_login_redirect( $url, $request, $user ) {
    if ( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
        if ( $user->has_cap( 'administrator' ) ) {
            $url = admin_url();
        } else {
            $url = home_url( '/setting/' );
        }
    }
    return $url;
}
 
add_filter( 'login_redirect', 'wpdocs_my_login_redirect', 10, 3 );

If you want to restrict the user to access the homepage use below code:

function redirect_user_to_page(){
    if ( is_user_logged_in() && is_front_page() ) {
     $user = wp_get_current_user();
     if ( !in_array( 'administrator', (array) $user->roles ) ) {
        wp_redirect('https://sample.com/setting');
        exit();
     }
     
    } else {
        // user not logged in, nothing
    }
}
add_action('wp_head', 'redirect_user_to_page');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.