Skip to:
Content
Pages
Categories
Search
Top
Bottom

Plugin update first_name, last_name in wp_usermeta on activation

  • Avatar of Peterverkooijen
    peterverkooijen
    Participant

    @peterverkooijen

    Continuing from this thread: Fresh start with a slightly shifted focus and more descriptive topic title.

    I’m trying to come up with a plugin that guarantees that first_name + last_name are stored in wp_usermeta for every user.

    At the moment BP only stores these values when users update their profile, so you can never count on them being there for other scripts, like a mailing list or event registration.

    The existing profile update synchronize function is an obvious starting point: xprofile_sync_wp_profile()

    This function takes the logged-in user’s fullname database field, splits it into first and last name, and stores the result in wp_usermeta.

    Instead of taking $fullname from the database I’ve tried to figure out how to take it from the registration form directly and store it on registration. That was a dead end. :-(

    Looking for examples in other plugins I see that Paul Gibbs’ Welcome Pack uses the wpmu_activate_user action. That should work for my purposes as well; every user that’s activated gets first and last name in wp_usermeta.

    Where do I get the $fullname from? The first function in the Welcome Pack plugin has these “$thingies” between the () – what do you call those?:

    function dp_welcomepack_welcomemessage( $user_id, $password, $meta )

    I know the input for fullname (field_1) is somehow included in that $meta stuff. How? I have no clue…

    Here’s what I have so far:

    function synchro_wp_usermeta($user_id, $meta) {
    global $bp, $wpdb;

    $fullname = GET FROM THE REGISTRATION FORM INPUT???
    $space = strpos( $fullname, ' ' );

    if ( false === $space ) {
    $firstname = $fullname;
    $lastname = '';
    } else {
    $firstname = substr( $fullname, 0, $space );
    $lastname = trim( substr( $fullname, $space, strlen($fullname) ) );
    }

    update_usermeta( $bp->loggedin_user->id, 'nickname', $fullname );
    update_usermeta( $bp->loggedin_user->id, 'first_name', $firstname );
    update_usermeta( $bp->loggedin_user->id, 'last_name', $lastname );

    $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET display_name = %s WHERE ID = %d", $fullname, $bp->loggedin_user->id ) );
    $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET user_url = %s WHERE ID = %d", bp_core_get_user_domain( $bp->loggedin_user->id ), $bp->loggedin_user->id ) );
    }
    add_action( 'wpmu_activate_user', 'synchro_wp_usermeta' );

    Getting another coffee now, digging further through the Welcome Pack plugin next. Any pointers very much appreciated!

Viewing 4 replies - 26 through 29 (of 29 total)
  • Avatar of Peterverkooijen
    peterverkooijen
    Participant

    @peterverkooijen

    @Mike Pratt, I’ve tried the honey approach in the past. I’m not even counting on responses anymore. I post my attempts here to keep track of what I’m doing and hopefully in the end the thread becomes useful for others in my situation.

    @DJPaul, thanks for the additional clue. I think I need to go back to that “flowchart of registration events in wpmu” and get a better insight what happens where in the process.

    Avatar of Peterverkooijen
    peterverkooijen
    Participant

    @peterverkooijen

    This is the function that the regular wpmu registration process uses to add the user to wp_usermeta:

    function update_usermeta( $user_id, $meta_key, $meta_value ) {
    global $wpdb;
    if ( !is_numeric( $user_id ) )
    return false;
    $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key);

    /** @todo Might need fix because usermeta data is assumed to be already escaped */
    if ( is_string($meta_value) )
    $meta_value = stripslashes($meta_value);
    $meta_value = maybe_serialize($meta_value);

    if (empty($meta_value)) {
    return delete_usermeta($user_id, $meta_key);
    }

    $cur = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s", $user_id, $meta_key) );
    if ( !$cur )
    $wpdb->insert($wpdb->usermeta, compact('user_id', 'meta_key', 'meta_value') );
    else if ( $cur->meta_value != $meta_value )
    $wpdb->update($wpdb->usermeta, compact('meta_value'), compact('user_id', 'meta_key') );
    else
    return false;

    wp_cache_delete($user_id, 'users');

    return true;
    }

    Is this function also used by Buddypress or does Buddypress replace it with something else?

    Where do meta_key and meta_value come from? Data from Buddypress xprofile field_1 is not included at this point…

    What would happen if I just added $bp as global? Trying that now… EDIT: Does nothing.

    I see xprofile_sync_wp_profile() uses this same function….

    Avatar of Peterverkooijen
    peterverkooijen
    Participant

    @peterverkooijen

    So how about this then, following DJPaul’s suggestions:

    function synchro_wp_usermeta($user_id, $password, $meta) {
    global $bp, $wpdb;

    $fullname = $meta[field_1];
    $space = strpos( $fullname, ' ' );

    if ( false === $space ) {
    $firstname = $fullname;
    $lastname = '';
    } else {
    $firstname = substr( $fullname, 0, $space );
    $lastname = trim( substr( $fullname, $space, strlen($fullname) ) );
    }

    update_usermeta( $user_id, 'nickname', $fullname );
    update_usermeta( $user_id, 'first_name', $firstname );
    update_usermeta( $user_id, 'last_name', $lastname );

    $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET display_name = %s WHERE ID = %d", $fullname, $user_id ) );
    $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET user_url = %s WHERE ID = %d", bp_core_get_user_domain( $user_id ), $user_id ) );
    }
    add_action( 'wpmu_activate_user', 'synchro_wp_usermeta', 10, 3);

    Can I simply replace $bp->loggedin_user->id with $user_id?

    Trying now…

    IT WORKS!!! HALLELUJA!!!

    Thanks DJPaul for all the essential clues!

    Avatar of h4x3d
    h4x3d
    Participant

    @h4x3d

    Peterverkooijen, thanks for the unserialize bit, I could really use that for my xprofile value problem.

Viewing 4 replies - 26 through 29 (of 29 total)

You must be logged in to reply to this topic.