Access User Meta Data on User Registration in Word

2019-09-01 02:42发布

问题:

I am attempting to carry out a few functions when a user registers on a wordpress site. I have created a module for this which carries out the following function:

add_action( 'user_register', 'tml_new_user_registered' );

function tml_new_user_registered( $user_id ) {
    //wp_set_auth_cookie( $user_id, false, is_ssl() );
    //wp_redirect( admin_url( 'profile.php' ) );
    $user_info = get_userdata($user_id);
        $subscription_value = get_user_meta( $user_id, "subscribe_to_newsletter", TRUE);
    if($subscription_value == "Yes") {
        //include("Subscriber.Add.php");    
    }


    echo "<pre>: ";
    print_r($user_info);
    print_r($subscription_value);
    echo "</pre>";

    exit;
}

But it seems that i am not able to access any user meta data as at the end of this stage none of it is stored.

Any ideas how i execute a function once Wordpress has completed the whole registration process of adding meta data into the relevant tables too?

I attempted to use this:

add_filter('user_register ','tml_new_user_registered',99);

But with no luck unfortunately.

Thanks in advance!

回答1:

I read at the action reference api page that the user id is passed as user ID. Try substituting your $user_id for $user_ID.



回答2:

I don't think the user metadata is available at the point where this action hook is triggered. From the Codex

"Not all user meta data has been stored in the database when this action is triggered. For example, nickname is in the database but first_name and last_name are not (as of 3.9.1). The password has already been encrypted when this action is triggered."



标签: wordpress