I have a "change password" page.
Is there a way to redirect that page when the user login first time
using wordpress generated password?
Thanks
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityI have a "change password" page.
Is there a way to redirect that page when the user login first time
using wordpress generated password?
Thanks
Maybe this helps you?
https://stackoverflow.com/questions/4267285/redirect-user-after-first-login-in-wordpress
He seems to had a similar problem and solved it a year ago.
What he does different to your approach is redirecting 48hrs after registration. Maybe this is enough?
If not - I'm looking to do it your way.
--- EDIT
Okay I just made a plugin what does exactly what you want:
/*
Plugin Name: Redirect Passwort
Plugin URI: TODO
Description: TODO
Author: xaeDes
Version: 0.1
Author URI: TODO
License: GPL2
*/
function redirect_passwort_profile_update($user_id, $old_user_data) {
$user = new WP_User( $user_id );
if( $user->data->user_pass != $old_user_data->user_pass) {
//password has changed
update_metadata("user",$user_id,"changed_password",true);
}
}
add_action("profile_update", "redirect_passwort_profile_update", 10, 2);
function redirect_passwort_login_redirect($redirect_to, $url_redirect_to = '', $user = null) {
if( isset($user->ID) ) {
$changed_password = get_metadata("user", $user->ID, "changed_password",true);
if( $changed_password != true ) {
return get_bloginfo('url') . "/change-your-password-dude/";
} else {
return $redirect_to;
}
}
}
add_filter('login_redirect', 'redirect_passwort_login_redirect',10,3);
function redirect_passwort_password_reset( $user ) {
//password has been reset to a random one. so the changed_password meta data should be reset as well
if( isset($user->ID) ) {
delete_metadata("user", $user->ID, "changed_password");
}
}
add_action('password_reset', 'redirect_passwort_password_reset');
It adds a user meta data "changed_password" to the user who changed its passwort.
When logging in it checks whether the user meta data "changed_password" is set and redirects if it is not set (and therefore the user has not changed its passwort even once).
When the users password is reset to a random one, the user meta data "changed_password" is reset as well.
return get_bloginfo('url') . "/change-your-password-dude/";
You are using wp_redirect to redirect to a site. But my plugin hooks into login_redirect, which is called from standard wordpress login. You could apply the login_redirect for yourself : wp_redirect(apply_filters('login_redirect', get_bloginfo('url') . '/my-account/','', $user););
in your pastebin line 19. Does that solve your problems?
change-password
page. But it still redirect to my-account
page. And yes i replaced your change-your-password-dude
text with my change-password
text. Any idea whats wrong?
Mar 25, 2012 at 18:16
redirect_passwort_profile_update
or redirect_passwort_login_redirect
are ever called? Maybe add something like this as first line of the files : file_put_contents(dirname(__FILE__).'/log-'.__FUNCTION__.'_'.__LINE__, '');
and check the folder of the plugin for generated files. The folder of the plugin have to be writable for the webserver.
Hook user_register and add user metadata to store a "has not logged in yet" flag. Hook wp_login and check for that metadata, delete and redirect if it exists, otherwise do normal login behavior.
In addition to Milo's answer:
function after_user_register( $user_id ){
// the new user just registered but never logged in yet
add_user_meta($user_id, "has_not_logged_in_yet", "true", true);
}
add_action( 'user_register', 'after_user_register', 10, 1 );
function after_user_loggedin(){
if(is_user_logged_in()){
$user_id=get_current_user_id();
$user_meta=get_user_meta($user_id);
if(isset($user_meta['has_not_logged_in_yet'])){
delete_user_meta($user_id, "has_not_logged_in_yet");
//do something else
}
}
}
add_action( 'init', 'after_user_loggedin');
Edit (FYI)
Hook wp_login
is not performing very well in terms of user meta retrieval. Sometimes returns FALSE
with the user loggedin.