Dario Ackermann Dario Ackermann - 1 year ago 210
PHP Question

Wordpress user_register hook not executing?

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)

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' ));

hook as well the
filter are working fine, but
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
gets fired.
How can I get the
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);


<th><label for="mobile_phone">Handynummer:</label></th>
<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 } ?>





Answer Source

user_register doesn't fire every time, on updating existing profile, profile_update is fired...

Just add your callback to both actions, so that it fires on updating too ;)

add_action( 'user_register', array($this, 'eddpremium_register_fields_save'), 10, 1 );
add_action( 'profile_update', array($this, 'eddpremium_register_fields_save'), 10, 1 );


WP checks if ID (user ID) is passed with form data, if it is, profile_update hook is fired. Otherwise user_register is.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download