5

Because it's not working for me. This code checks if a user has just registered. I want to redirect him to a custom page if so. Otherwise, redirect him to the homepage or admin page.

function mylogin_redirect($redirect_to, $url_redirect_to = '', $user = null) {
    if( $user->ID ) {
        $user_info = get_userdata( $user->ID ); 
        // If user_registered date/time is less than 48hrs from now
        // Message will show for 48hrs after registration
        if ( strtotime( $user_info->user_registered ) > ( time() - 172800 ) ) {
            return get_bloginfo('url') . "/custompage/";
        } elseif( current_user_can( 'manage_options' )) {
            return admin_url();
        } else {
            return get_bloginfo('url');
        }
    }
}
add_filter('login_redirect', 'mylogin_redirect');

I get the expected results for the two options but the admin. elseif( current_user_can( 'manage_options' )) { return admin_url(); } doesn't seem to get parsed.

7

Probably because the global $current_user isn't valid yet, which is used by current_user_can().

However, you can use this instead;

if ($user->has_cap('manage_options')) { return admin_url(); }

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.