Domanda

I want to redirect my users to a page after they logged in for the first time. I also noticed some solutions like a redirect after the user was registered (redirect after first 48 hours) but the problem is that all users are registered already.

Does anyone knows how to do this?

Thanks!

È stato utile?

Soluzione

Use following code to achieve your requirement.

//hook when user registers
add_action( 'user_register', 'myplugin_registration_save', 10, 1 );

function myplugin_registration_save( $user_id ) {

    // insert meta that user not logged in first time
    update_user_meta($user_id, 'prefix_first_login', '1');

}

// hook when user logs in
add_action('wp_login', 'your_function', 10, 2);

function your_function($user_login, $user) {

    $user_id = $user->ID;
    // getting prev. saved meta
    $first_login = get_user_meta($user_id, 'prefix_first_login', true);
    // if first time login
    if( $first_login == '1' ) {
        // update meta after first login
        update_user_meta($user_id, 'prefix_first_login', '0');
        // redirect to given URL
        wp_redirect( 'http://www.example.com/' );
        exit;
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a wordpress.stackexchange
scroll top