phpwordpresswordpress-admin

Wordpress Dashboard redirect for specific User Role


I'm successfully using this code to redirect /wp-admin/index.php (WP Dashboard) to another specific Admin page. I would now like to limit this action to a specific user role (i.e Author).

add_action( 'admin_init', function () {

    global $pagenow;

    # Check current admin page.
    if ( $pagenow == 'index.php' ) {

        wp_redirect( admin_url( '/admin.php?page=wuapc-page-376' ) );
        exit;
    }
} );

Solution

  • use wp_get_current_user

    add_action( 'admin_init', function () {
    
        global $pagenow;
        $user = wp_get_current_user();
    
        # Check current admin page.
        if ( $pagenow == 'index.php' && in_array('author', $user->roles)) {
    
            wp_redirect( admin_url( '/admin.php?page=wuapc-page-376' ) );
            exit;
        }
    } );