Ich fragte mich, ob hier jemand helfen könnte.
Grundsätzlich habe ich ein benutzerdefiniertes Registrierungsformular erstellt, das nach der Validierung einen Benutzer in die Benutzertabelle einfügt.
function _new_user($data) {
// Separate Data
$default_newuser = array(
'user_pass' => wp_hash_password( $data['user_pass']),
'user_login' => $data['user_login'],
'user_email' => $data['user_email'],
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'role' => 'pending'
);
wp_insert_user($default_newuser);
}
Jetzt muss ich nur noch die Bestätigungs-E-Mail senden, die ich mit dem folgenden Code ausführen kann.
wp_new_user_notification($user_id, $data['user_pass']);
Ich möchte stattdessen eine Benutzeraktivierungs-E-Mail senden. Ich habe ein paar Dinge ausprobiert, aber ich kann anscheinend nichts Konkretes finden. Ich hoffe, dass jemand dieses Problem schon einmal hatte.
Um den Benutzeraktivierungsprozess abzuschließen, müssen Sie die folgenden Schritte ausführen:
Benutzerdefiniertes Feld hinzufügen und E-Mail senden:
function _new_user($data) {
// Separate Data
$default_newuser = array(
'user_pass' => wp_hash_password( $data['user_pass']),
'user_login' => $data['user_login'],
'user_email' => $data['user_email'],
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'role' => 'pending'
);
$user_id = wp_insert_user($default_newuser);
if ( $user_id && !is_wp_error( $user_id ) ) {
$code = sha1( $user_id . time() );
$activation_link = add_query_arg( array( 'key' => $code, 'user' => $user_id ), get_permalink( /* YOUR ACTIVATION PAGE ID HERE */ ));
add_user_meta( $user_id, 'has_to_be_activated', $code, true );
wp_mail( $data['user_email'], 'ACTIVATION SUBJECT', 'CONGRATS BLA BLA BLA. HERE IS YOUR ACTIVATION LINK: ' . $activation_link );
}
}
Benutzeraktivierung beim Login überprüfen:
// override core function
if ( !function_exists('wp_authenticate') ) :
function wp_authenticate($username, $password) {
$username = sanitize_user($username);
$password = trim($password);
$user = apply_filters('authenticate', null, $username, $password);
if ( $user == null ) {
// TODO what should the error message be? (Or would these even happen?)
// Only needed if all authentication handlers fail to return anything.
$user = new WP_Error('authentication_failed', __('<strong>ERROR</strong>: Invalid username or incorrect password.'));
} elseif ( get_user_meta( $user->ID, 'has_to_be_activated', true ) != false ) {
$user = new WP_Error('activation_failed', __('<strong>ERROR</strong>: User is not activated.'));
}
$ignore_codes = array('empty_username', 'empty_password');
if (is_wp_error($user) && !in_array($user->get_error_code(), $ignore_codes) ) {
do_action('wp_login_failed', $username);
}
return $user;
}
endif;
Aktivierungsseite:
add_action( 'template_redirect', 'wpse8170_activate_user' );
function wpse8170_activate_user() {
if ( is_page() && get_the_ID() == /* YOUR ACTIVATION PAGE ID HERE */ ) {
$user_id = filter_input( INPUT_GET, 'user', FILTER_VALIDATE_INT, array( 'options' => array( 'min_range' => 1 ) ) );
if ( $user_id ) {
// get user meta activation hash field
$code = get_user_meta( $user_id, 'has_to_be_activated', true );
if ( $code == filter_input( INPUT_GET, 'key' ) ) {
delete_user_meta( $user_id, 'has_to_be_activated' );
}
}
}
}
Dies ist Ihr Ausgangspunkt, gehen Sie vor und passen Sie es an Ihre Bedürfnisse an.
Zwei Möglichkeiten zur Auswahl:
Verwenden Sie ein Plugin, zum Beispiel Benutzeraktivierungs-E-Mail oder Neuer Benutzer genehmigt
Code dies selbst.
Einige Funktionen, mit denen Sie beginnen sollten:
Sie können die user_id erhalten, indem Sie dies während der Authentifizierung tun:
$username='user email provided by the user at login panel.';
$results = $wpdb->get_row( "SELECT ID FROM wp_users WHERE user_email='".$username."'");
$activation_id = $results->ID;
$activation_key = get_user_meta( $activation_id, 'has_to_be_activated', true );
if($activation_key != false )
{
echo '<h4 class="error">Your account has not been activated yet.<br /> To activate it check your email and clik on the activation link.</h4>';
}
else{
//authenticate your user login here...
}