Wordpress user_register hook not executing?

2019-07-17 06:22发布

问题:

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

}

回答1:

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

UPDATE

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



回答2:

I am new to wordpress plugin development too. But since the 'eddpremium_register_fields_save' doesn't work correctly when called in an add_action method why not define an anonymous function inside the add_action methode? I think that the argument that 'eddpremium_register_fields_save' must have is what is causing problem in your call.