I'm creating a Worldpress Plugin for my own purpose. I'd like to add some additional fields to the registration form (First Name, Last Name and Phone Number)
<?php
function __construct() {
....
add_action( 'register_form', array($this, 'eddpremium_register_additonalfields' ));
add_filter( 'registration_errors', array($this, 'eddpremium_register_errors'), 10, 3 );
add_action( 'user_register', array($this, 'eddpremium_register_fields_save'), 10, 1 );
....
add_action( 'user_contactmethods', array($this, 'eddpremium_mobilenumber_setting' ));
}
The register_form
hook as well the registration_errors
filter are working fine, but user_register
doesn't. It seems, that the function, which should save the additonal fields, doesn't get fired...
function eddpremium_register_fields_save($user_id) {
$first_name = $_POST["first_name"];
$last_name = $_POST["last_name"];
$mobile_phone = $_POST["mobile_phone"];
// No need to check, variables get checked in registration_errors
update_user_meta($user_id, "first_name", $first_name);
update_user_meta($user_id, "last_name", $last_name);
update_user_meta($user_id, "mobile_phone", $mobile_phone);
//$username = get_userdata($user_id)->user_login;
}
Strangley, after the successful registration user_contactmethods
gets fired.
How can I get the user_register
hook to work properly?
Picture of the problem causing:
(This function shouldn't be fired after registration, however it get's fired)
Code of the function which shouldn't get executed at that point:
function eddpremium_mobilenumber_setting() {
$userid = $_GET["user_id"]; // Zu bearbeitende Nutzerid
if(empty($userid)) // wenn aktueller Nutzer eigenes Profil bearbeitet
$userid = get_current_user_id();
$mobile_phone_verified = get_the_author_meta('mobile_phone_verified', $userid);
?>
<tr>
<th><label for="mobile_phone">Handynummer:</label></th>
<td>
<input id="mobile_phone" name="mobile_phone" autocomplete="off" <?php if(empty($mobile_phone_verified) AND !current_user_can( 'manage_options' )) echo 'disabled="disabled"'; ?> placeholder="0791112233" type="tel" value="<?php $mobile_phone = get_the_author_meta( 'mobile_phone', $userid );
echo $mobile_phone ? $mobile_phone : '';?>" />
<?php if(empty($mobile_phone_verified)) {?> <a style='color:red;'>Verifikation erforderlich</a> <?php } else {?>
<a style='color:green;'>Nummer verifiziert</a>
<?php } ?>
</td>
</tr>
<?php
}