Question

I'm trying to give the role 'subscriber' the ability to edit_post. While this is successful I cannot seem to direct users away from the default wordpress wp-admin page upon signing in.

Whenever I try a suggested solution users who login (with edit_post permission) get redirected to wp-admin page.

When they sign in no matter what I do wordpress forwards them to wp-admin showing the wordpress backend. (I do not want users to see this)

The reason why I want to do this is because I have a custom dashboard for them to use located at http://website.com/login/customdashboard/

I'd prefer this to be a function.

I've tried plugins with no success:
"Peter's Login Redirect" & also the "s2member (Login Welcome Page)"

I TRIED THE FOLLOWING FUNCTION WITH NO SUCCESS:

function soi_login_redirect( $redirect_to, $request, $user  ) {
    return ( is_array( $user->roles ) && in_array( 'administrator', $user->roles ) ) ? admin_url() : site_url();
} // end soi_login_redirect
add_filter( 'login_redirect', 'soi_login_redirect', 10, 3 );

ANOTHER TRIED WITH NO SUCCESS:

function redirect_login_page(){
$page_viewed = basename($_SERVER['REQUEST_URI']);
 $login_page  = site_url();
 if( $page_viewed == "wp-login.php" && $_SERVER['REQUEST_METHOD'] == 'GET') {
 wp_redirect($login_page);
 exit();
 }
}
add_action('init','redirect_login_page');

Any help in this regard would be greatly appreciated.

Was it helpful?

Solution

If you want to redirect all /wp-admin address you can use:

add_action( 'admin_init', 'redirect_so_15396771' );

function redirect_so_15396771()
{   
    if ( defined( 'DOING_AJAX' ) && DOING_AJAX )  
        return;

    // http://codex.wordpress.org/Roles_and_Capabilities
    if ( !current_user_can('delete_users') ) {
            wp_redirect( site_url( '/login/customdashboard/' ) );
            exit();
    }
}

If you need to filter this according to the current page, you can use the global $pagenow, which has the values index.php, profile.php, tools.php, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top