0

I'm trying to access the $user->first_name property from a function triggered by the register_user action hook but it's not there:

function do_stuff($user_id) {

$new_user = get_userdata($user_id);

$first_name1 = $new_user->user_firstname;
$last_name1 = $new_user->user_lastname;
    echo "<" . $first_name1 . $last_name1 . ">";
    //returns: <>

$first_name2 = $new_user-first_name;
$last_name2 = $new_user->last_name;
    echo "<" . $first_name2 . $last_name2 . ">";
    //returns: <>

}
add_action("register_user", "do_stuff");

UPDATE:

I tried this:

$user_meta = get_user_meta( $new_user->ID );
var_dump($user_meta);

and I got this (last_name and first_name are empty even though they're defined in the user's profile):

array(11) { ["wp_user_level"]=> array(1) { [0]=> string(1) "0" } ["show_admin_bar_front"]=> array(1) { [0]=> string(4) "true" } ["wp_capabilities"]=> array(1) { [0]=> string(32) "a:1:{s:10:"subscriber";s:1:"1";}" } ["use_ssl"]=> array(1) { [0]=> string(1) "0" } ["admin_color"]=> array(1) { [0]=> string(5) "fresh" } ["comment_shortcuts"]=> array(1) { [0]=> string(5) "false" } ["rich_editing"]=> array(1) { [0]=> string(4) "true" } ["description"]=> array(1) { [0]=> string(0) "" } ["nickname"]=> array(1) { [0]=> string(7) "emerson" } ["last_name"]=> array(1) { [0]=> string(0) "" } ["first_name"]=> array(1) { [0]=> string(0) "" } }

2 Answers 2

0

I think some user stuff changed in 3.2 and perhaps user meta works a bit differently now...

function do_stuff( $user_id ) {
    $first_name = get_user_meta( $user_id, 'first_name', true );
    $last_name = get_user_meta( $user_id, 'last_name', true );
    echo $first_name . $last_name;
}
add_action( 'user_register', 'do_stuff' );
11
  • I also get nothing from $first_name = get_user_meta( $user_id, 'first_name', true ); I've had all of these work in the past so I'm totally confused. Aug 17, 2012 at 14:39
  • @Emerson - are you sure you're getting nothing? outputting something within non-encoded angle brackets <> will hide it from view in a browser. tested and working for me in TwentyTen theme.
    – Milo
    Aug 17, 2012 at 15:02
  • @Emerson - also verify you are using the correct action, it's user_register not register_user as in your code example.
    – Milo
    Aug 17, 2012 at 15:06
  • Thanks for clarifying. That was a typo (I'm using user_register. Aug 17, 2012 at 15:40
  • I updated the question with the output of get_user_meta(). (Thanks for your help!) Aug 17, 2012 at 15:41
0

There has been no new answer to the question for years. Maybe the solution I applied in a similar problem will be useful to you. When Wordpress creates an user, it first creates the user and then creates the meta datas. Therefore, you may not be able to access all information with 'user_register'. You can use the 'profile_update' action instead. This action is fired after the user is created and the last meta data is saved.

function my_custom_function ( $user_id ) {

    $user_data = get_userdata( $user_id );

    $first_name = $user_data->first_name;
    $last_name = $user_data->last_name;

}
add_action('profile_update', 'my_custom_function');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.