I'm working on a WP-based site that requires creating accounts for users and sending them their password reset links by email so they can activate their accounts. After manually sending out all the emails, the users set their passwords with no error but they cannot log in to the site.
This is my code:
define('WP_USE_THEMES', false);
require('wp-load.php');
require_once ABSPATH . WPINC . '/class-phpass.php';
$recepients = get_users( 'blog_id=1&orderby=nicename&role=test' ); //Retrieves users of specific role
foreach ( $recepients as $user ) {
$key = wp_generate_password( 20, false );
do_action( 'retrieve_password_key', $user->user_login, $key );
$wp_hasher = new PasswordHash( 8, true );
$hashed = time() . ':' . $wp_hasher->HashPassword( $key );
$wpdb->update( $wpdb->users, array( 'user_activation_key' => $hashed ), array( 'user_login' => $user->user_login ) );
if ( apply_filters( 'send_new_user_notification', true ) ) {
$message = 'A new account has been created for you. Please click the following link: <a href="http://www.mysite.gr/activation/?key='.$key.'&login='.$user->user_login.'">ACTIVATE YOUR ACCOUNT</a>';
$title = sprintf( '%s - Your account activation', wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ) );
$title = apply_filters( 'new_user_notification_title', $title, $user_id );
$message = apply_filters( 'new_user_notification_message', $message, $key, $user_id );
$headers = array(
'Content-Type: text/html; charset=UTF-8',
'From: ME <[email protected]>',
);
$send_message = wp_mail( $user->user_email, $title, $message, $headers );
}
}
I'm using WP v4.3.1 a number of plugins for keeping all the user interaction on the front-end (I dont want them logging into their profiles).
One idea I have is to perhaps update the user_activation_key field in wp_users table based on the key that has already been sent so that I wont have to re-send the activation email twice. Is this at all posible? I mean is there a way to 'fix' the user_activation_key in the DB by somehow using the keys already sent to the users?
ANY help would be greatly appreciated.