Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In multisite this code redirect users (authors) to /wp-admin/post-new.php after the login ....but also redirects me (super admin) how can I prevent this?

function primary_login_redirect( $redirect_to, $request_redirect_to, $user )
{
    if ($user->ID != 0) {
        $user_info = get_userdata($user->ID);
        if ($user_info->primary_blog) {
            $primary_url = get_blogaddress_by_id($user_info->primary_blog) . 'wp-admin/post-new.php';
            if ($primary_url) {
                wp_redirect($primary_url);
                die();
            }
        }
    }
    return $redirect_to;
} 
add_filter('login_redirect','primary_login_redirect', 100, 3);
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

You can test against is_super_admin(). The following works in my testing:

function primary_login_redirect( $redirect_to, $request_redirect_to, $user ) {
    if ( is_a( $user, 'WP_User' ) && ! is_super_admin( $user->ID ) ) {
        if ( $user_info->primary_blog ) {
        $user_info = get_userdata( $user->ID );
            $primary_url = get_blogaddress_by_id( $user_info->primary_blog ) . 'wp-admin/post-new.php';
            if ( $primary_url ) {
                wp_redirect( $primary_url );
                die();
            }
        }
    }
    return $redirect_to;
} 
add_filter( 'login_redirect', 'primary_login_redirect', 100, 3 );

The is_a() check clears some notices I got when logged out.

EDIT: If you want to do a different redirect URL for super admins only, I'd restructure to something like this:

function primary_login_redirect( $redirect_to, $request_redirect_to, $user ) {
    if ( is_a( $user, 'WP_User' ) ) {
        if ( ! is_super_admin( $user->ID ) ) {
            $user_info = get_userdata( $user->ID );
            if ( $user_info->primary_blog )
                $primary_url = get_blogaddress_by_id( $user_info->primary_blog ) . 'wp-admin/post-new.php';
        } else { // super admins
            $primary_url = network_admin_url( 'sites.php' );
        }
        if ( $primary_url ) {
            wp_redirect( $primary_url );
            die();
        }
    }
    return $redirect_to;
} 
add_filter( 'login_redirect', 'primary_login_redirect', 100, 3 );
share|improve this answer
    
thank you! I would like to redirect the super admin to /wp-admin/network/sites.php ...how can i do that? thanks ! –  Ghinnersmee May 26 '13 at 19:47
    
You can just add an else with your separate redirect just before you return $redirect_to to the filter. –  DrewAPicture May 26 '13 at 20:02
    
It's worth mentioning that if you want to add an else, you might do better to split the first double test (the is_a() and ! is_super_admin()) so you test against the is_a() first, then inside that the is_super_admin(), if that makes sense. That way, when you do the else, you still avoid the WP_Errors generated for logged-out users. –  DrewAPicture May 26 '13 at 20:08
    
I get errors :( ....can you please rewrite the entire code with super admin redirect ? please :( –  Ghinnersmee May 26 '13 at 20:14
    
Try the second snippet in the edit. –  DrewAPicture May 26 '13 at 20:22
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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