Skip to:
Content
Pages
Categories
Search
Top
Bottom

Plugin update first_name, last_name in wp_usermeta on activation


  • 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 25 replies - 1 through 25 (of 29 total)
  • Hey, the entire best way to get a response from me is to give my plugins some advertising!

    Let’s see. Those thingies are called arguments. As you have probably figured out, ‘wpmu_activate_user’ action is called from wpmu-functions.php line 1208 (on my development server files, at least).

    First two arguments are obvious. When an account is registered, a new records is made in the wp_signups database table. There is a column called $meta, which is a seralized PHP array. $meta as in the wpmu_activate_user argument, is the unseralized version of that array for the user that has just activated his/her account.

    On one of my test users, it seems to contains keys: field_1, xprofile_fields_ids (not sure), avatar_image_resized (bool) and avatar_image_original (bool).

    So as you’ve suggested above, I reckon you can just use $meta[‘field_1’]. Let us know!

    EDIT: the code you’ve pasted above. You’ll need to have all three arguments, even if you don’t want to use them all. You can miss out ones AFTER the one you want, but you can’t miss anything out before the one you wish to use.


    peterverkooijen
    Participant

    @peterverkooijen

    Thanks Paul!

    $meta[‘field_1’] definitely looks like something to try, although I still totally don’t “get” serialized PHP arrays.

    I was also wondering if I could just bp_user_fullname() at this point, before activation.

    Trying both now…

    EDIT:

    function synchro_wp_usermeta($user_id, $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( $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' );

    ?>

    Registration process works fine, but I with this error in the screen on activation:

    Warning: Missing argument 2 for synchro_wp_usermeta() in /serverpath/wp-content/mu-plugins/bp-custom.php on line 187

    I had taken $password out. Thought I didn’t need it. Will try again with it added back in….

    EDIT2: Adding $password only gives me a second error:

    Warning: Missing argument 2 for synchro_wp_usermeta() in /serverpath/wp-content/mu-plugins/bp-custom.php on line 187

    Warning: Missing argument 3 for synchro_wp_usermeta() in /serverpath/wp-content/mu-plugins/bp-custom.php on line 187

    Can I just take $user_id out? Is all the information I need already in that wpmu_activate_user action?

    I’ll try that next, after deleting test users. Running out of email addresses…


    peterverkooijen
    Participant

    @peterverkooijen

    This latest attempt had no error messages, but also no first_name and last_name were added to wp_usermeta:

    function synchro_wp_usermeta($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( $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' );

    ?>

    I suspect I need some additional code to “unpack” $meta.

    In a previous attempt I tried this, based on a function in bp-xprofile-signup.php:

    // Extract signup meta fields to fill out profile
    $field_ids = $meta['xprofile_field_ids'];
    $field_ids = explode( ',', $field_ids );

    // Loop through each bit of profile data and save it to profile.
    for ( $i = 0; $i < count($field_ids); $i++ ) {
    if ( empty( $field_ids[$i] ) ) continue;

    $field_value = $meta["field_{$field_ids[$i]}"];

    $field = new BP_XProfile_ProfileData();
    $field->user_id = $user_id;
    $field->value = $field_value;
    $field->field_id = $field_ids[$i];
    $field->last_updated = time();
    }

    $fullname = $field->field_1;

    I think it crashed my server…

    I’ll need help (un?)serializing that $meta array. I’ll study up on that stuff later.

    But first I’ll try if bp_user_fullname() works. After some late lunch…


    peterverkooijen
    Participant

    @peterverkooijen

    You’ll need to have all three arguments, even if you don’t want to use them all. You can miss out ones AFTER the one you want, but you can’t miss anything out before the one you wish to use.

    Are you sure about that? With the three arguments I get two ‘Missing argument’ error messages. With only $meta there are no errors – although it doesn’t do anything either…

    EDIT: Tried the code below. Again one ‘Missing argument’ error. Registration goes through as normal, but nothing in wp_usermeta:

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

    $field_ids = $meta['xprofile_field_ids'];
    $field_ids = explode( ',', $field_ids );

    // Loop through each bit of profile data and save it to profile.
    for ( $i = 0; $i < count($field_ids); $i++ ) {
    if ( empty( $field_ids[$i] ) ) continue;

    $field_value = $meta["field_{$field_ids[$i]}"];
    }

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


    peterverkooijen
    Participant

    @peterverkooijen

    Stuck again…

    I think this is the function that takes the input from the registration form (in bp_xprofile_classes.php):

    function get_signup_fields() {
    global $wpdb, $bp;

    $sql = $wpdb->prepare( "SELECT f.id FROM {$bp->profile->table_name_fields} AS f, {$bp->profile->table_name_groups} AS g WHERE g.name = %s AND f.parent_id = 0 AND g.id = f.group_id ORDER BY f.id", get_site_option('bp-xprofile-base-group-name') );

    if ( !$temp_fields = $wpdb->get_results($sql) )
    return false;

    for ( $i = 0; $i < count($temp_fields); $i++ ) {
    $fields[] = new BP_XProfile_Field( $temp_fields[$i]->id, null, false );
    }

    return $fields;
    }

    Then bp-xprofile-signup.php has the function xprofile_load_signup_meta() with this bit:

    $fields = BP_XProfile_Field::get_signup_fields();

    if ( $fields ) {
    foreach ( $fields as $field ) {

    $value = $_POST['field_' . $field->id];

    Followed by lots of validation code. For my purposes I could end it here:

    $fullname = $_POST['field_1'];

    Trying to put it together:

    function synchro_wp_usermeta() {
    global $bp_user_signup_meta, $bp, $wpdb;

    $fields = BP_XProfile_Field::get_signup_fields();

    if ( $fields ) {
    foreach ( $fields as $field ) {

    $value = $_POST['field_' . $field->id];

    }
    }

    $fullname = $value['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( $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( 'user_register', 'synchro_wp_usermeta' );

    Does this make sense? Can any of the more experienced php-coders please point out the obvious mistakes?


    peterverkooijen
    Participant

    @peterverkooijen

    The last attempt in the previous post does not work. No error messages, registration goes through as normal, but nothing in wp_usermeta.

    This also has no effect:

    function synchro_wp_usermeta() {
    global $bp_user_signup_meta, $bp, $wpdb;

    $fields = BP_XProfile_Field::get_signup_fields();

    if ( $fields ) {
    foreach ( $fields as $field ) {

    $fullname = $_POST['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( $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( 'user_register', 'synchro_wp_usermeta' );

    Please, I need feedback to finish this function/plugin. A real coder can spot the problem probably immediately. Please point it out, don’t leave me hanging.

    add_action( 'wpmu_activate_user', 'synchro_wp_usermeta', 10, 3);

    function synchro_wp_usermeta( $user_id, $password, $meta ) { ... }

    You’re also going to need to hook into the ‘wpmu_activate_blog’ action, as activate_user is not called when the user chooses to create a blog on registration.


    peterverkooijen
    Participant

    @peterverkooijen

    Thanks for the additional clues DJPaul!

    In the latest attempt I was going back to my first approach, trying to catch the input straight from the form, so that has to run on initial user registration, not on activation.

    Or is user_register not used in Buddypress at all? Is there a wpmu or bp equivalent I could try? Should I use wpmu_create_user? According to this blog post:

    the do_action for wpmu_new_user is directly at the bottom of wpmu_create_user in the wp-includes/wpmu-functions.php the wpmu_create_user gets sent the username password and email and checks if the username or email exists, if not create the new user in the database and assign a user_id to the return, also assigning capabilities and level … this happens on *all* levels of registration and is the perfect hook point to add them into the database

    the do_action do_action( ‘wpmu_new_user’, $user_id ); so you get the immediate user_id soon as it’s created which you can use in your table

    Apparantly this is like a flowchart of registration events in wpmu (wp-includes/wpmu-default-filters.php):

    add_filter ( 'wpmu_validate_user_signup', 'signup_nonce_check' );
    add_action ( 'init', 'maybe_add_existing_user_to_blog' );
    add_filter ( 'xmlrpc_methods', 'attach_wpmu_xmlrpc' );
    add_filter ( 'wp_authenticate_user', 'wordpressmu_authenticate_siteadmin', 10, 2 );
    add_action ( 'wpmu_new_user', 'newuser_notify_siteadmin' );
    add_action ( 'wpmu_activate_user', 'add_new_user_to_blog', 10, 3 );
    add_action ( 'sanitize_user', 'strtolower_usernames', 10, 3 );

    Should I focus on these wpmu action hooks instead of regular wp hook? Does bp have a list like this somewhere?

    I couldn’t figure out how to serialize that $meta data and extract the fullname from it, so I abandoned the approach hooking into wpmu_activate_user for now. Also when I put in the $user_id and $password arguments I got the error messages about missing arguments. wtf?!

    I think this bit of code grabs the input from the registration form:

    $fields = BP_XProfile_Field::get_signup_fields();

    if ( $fields ) {
    foreach ( $fields as $field ) {

    $value = $_POST['field_' . $field->id];

    fullname is the input from field_1, but I don’t know how to finish the php to get to $fullname = … . The two latest attempts above did not work.

    I have to give up for now. Deadline at my day job coming up…

    Also when I put in the $user_id and $password arguments I got the error messages about missing arguments.

    add_action( ‘wpmu_activate_user’, ‘synchro_wp_usermeta’, 10, 3)


    peterverkooijen
    Participant

    @peterverkooijen

    You mean the 3 at the end of that line would fix that? I’ll try that next with a previous version of my code.

    Just for the record, this attempt again didn’t work. No errors, but nothing in wp_usermeta either:

    function synchro_wp_usermeta() {
    global $bp_user_signup_meta, $bp, $wpdb;

    $fields = BP_XProfile_Field::get_signup_fields();

    if ( $fields ) {
    foreach ( $fields as $field ) {

    $value = $_POST['field_' . $field->id];

    }
    }

    $fullname = $value['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( $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_new_user', 'synchro_wp_usermeta' );

    This fixes the missing arguments errors, but still nothing in wp_usermeta. I’ll need to figure out how to serialize (?) $meta and extract $fullname from it:

    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( $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', 10, 3);


    peterverkooijen
    Participant

    @peterverkooijen

    I had a good feeling about this attempt, but still nothing in wp_usermeta:

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

    $field_ids = $meta['xprofile_field_ids'];
    $field_ids = explode( ',', $field_ids );

    // Loop through each bit of profile data and save it to profile.
    for ( $i = 0; $i < count($field_ids); $i++ ) {
    if ( empty( $field_ids[$i] ) ) continue;

    $field_value = $meta["field_{$field_ids[$i]}"];
    }

    $fullname = $field_value['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( $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', 10, 3);

    This version messes up registration:

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

    $fields = BP_XProfile_Field::get_signup_fields();

    $field_ids = $meta['xprofile_field_ids'];
    $field_ids = explode( ',', $field_ids );

    // Loop through each bit of profile data and save it to profile.
    for ( $i = 0; $i < count($field_ids); $i++ ) {
    if ( empty( $field_ids[$i] ) ) continue;

    $field_value = $meta["field_{$field_ids[$i]}"];
    }

    $fullname = $field_value['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( $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', 10, 3);

    Registration goes through as normal. The user gets one email with the activation link. But the page on the link says ‘Your account has already been activated. You can now log in with the account details that were emailed to you.’ No email arrives.

    So the above code hijacks activation without doing anything (probably because $bp_user_signup_meta was added). Still nothing in wp_usermeta either.

    My problem is getting the $fullname from $meta. I don’t understand the array stuff. I’m basically just guessing.

    If there are any php coders out there that can spot any obvious mistakes or can suggest other things to try, please help me out.


    peterverkooijen
    Participant

    @peterverkooijen

    This version also doesn’t work. No errors, registration goes through as normal, but still no first_name and last_name in wp_usermeta:

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

    $field_ids = $meta['xprofile_field_ids'];
    $field_ids = explode( ',', $field_ids );

    // Loop through each bit of profile data and save it to profile.
    for ( $i = 0; $i < count($field_ids); $i++ ) {
    if ( empty( $field_ids[$i] ) ) continue;

    $field_value = $meta["field_{$field_ids[$i]}"];

    $field = new BP_XProfile_ProfileData();
    $field->user_id = $user_id;
    $field->value = $field_value;
    $field->field_id = $field_ids[$i];
    $field->last_updated = time();

    $field->save();
    }

    $fullname = $field_value['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( $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', 10, 3);

    Again, any suggestions that can move this along are very much appreciated.

    The problem really is how to extract the fullname data from the meta field in wp_signups. Can I do something similar to this:

    xprofile_get_field_data( BP_XPROFILE_FULLNAME_FIELD_NAME, $bp->loggedin_user->id );

    Is there a wp_signups_get_field_data I can tap into?

    Or some type of database query?:

    $wpdb->read( $wpdb->signups, array('???' => 1), array('??' => $key) );


    Mariusooms
    Participant

    @mariusooms

    Hi Peterverkooijen, sorry you are struggling with this for a long time now. I’m not sure if you absolutely need this for bp1.0.3, but if you study the registration process of the upcoming bp, you’ll see it is much easier to manipulate registration data at a theme level.

    It might be best worth your time to study the new code and wait a bit longer. Or work with trunk if you are developing.


    Mike Pratt
    Participant

    @mikepratt

    Am I missing something? All I had to do to achieve a solution to this was to create two new fields in basic called FirstName and LastName and make them mandatory on signup. Now I can ref them all I want in code later. What is the point of this huge plugin? Thee is no need to be dependent on users updating their profile since they can join without filling in these fields.


    peterverkooijen
    Participant

    @peterverkooijen

    Mike Pratt, you’re right, that is one way to do it. Then you have the following fields on your registration form: username, name, first name, last name. That is an excellent solution if your aim is to annoy potential members.

    Another option is to use the default “fullname” field for first name and create a second one for last name, but then you’re kinda messing with the system and perhaps causing problems in the future. I have already done this solution in an earlier version. It’s my fallback option.

    But why would I have to create custom fields when WordPress already has the first_name and last_name fields in wp_usermeta? And there is even a ready-to-use Buddypress function that synchronizes the fields in wp_usermeta with the fullname in BP.

    Unfortunately at the moment that function is only run when the user updates his profile, so it’s completely useless. I’m trying to tweak it and make it useful and introduce to Buddypress the wonders of a first_name, last_name in the database. (Wow, I know…)

    Also I’d like to get rid of the username on the form, by autogenerating it from the fullname – make lowercase, take out spaces, store as username. That should be a relatively easy next step once I’ve figured out how to use the fullname input from the registration form in a function.

    Also I wasn’t able to get bp tags working in a plugin, where I had no problem pulling first_name and last_name from wp_usermeta, providing they were available…

    This should be a really easy plugin for any php coder, but as Jeff Sayre has pointed out, having a somewhat clean, professional user registration in Buddypress is reserved for those well-versed in the codebase or with deep-enough pockets.

    @Mariusooms, I need to finish the site before Labor Day, with a related event on September 22nd. When will bp1.0.3 be released?

    I’ll figure it out somehow, with or without the help of WordPress insiders.


    Mariusooms
    Participant

    @mariusooms

    BP1.1 will be released in September pending development speed. I’m inclined to agree with Mike Pratt. Especially if you have a deadline that close, your solution seems very elaborate even trying to use the fullname function from BP. There is no guarantee the first and last name are existing in the username, so you can’t back on that info to be in usermeta. The only way to guarantee accurate first and last name info is to have those in user registration as a mandatory field.

    Bypassing the username and using email as a login option further removes you from even having remotely any user information other than the email address.

    Sorry to sound a little discouraging, but I’m scratching my head as to how you are trying to achieve this? Perhaps a combination with the email plugin and two required fields for first and last name is your best option, imho.

    That way you do need another plugin that needs development after you release your project and stays inline with bp updates and what not.


    peterverkooijen
    Participant

    @peterverkooijen

    The ideal registration form would look like this:

    – fullname

    – email address

    Both fields would of course be mandatory. Ideally fullname would be checked for two or more parts, but xprofile_sync_wp_profile() can handle one part input and I can live with an occassional missing last name.

    I use a jQuery validation script on another form in my site, that I may also be able to use on the registration form. As far I understand it doesn’t interfere with anything on the server side.

    Username would be generated behind the scenes from fullname.

    Many other member information fields would be optional under edit profile.

    If I can figure out how to select fullname from meta in wp_signups, adapting xprofile_sync_wp_profile() shouldn’t be that difficult.

    Or are my latest attempts above still nowhere near a solution? If you see the obvious mistakes, please point them out.


    peterverkooijen
    Participant

    @peterverkooijen

    When I do this:

    $fullname = $wpdb->get_var("SELECT meta FROM $wpdb->signups");
    echo $fullname;

    I get the serialized content of the first meta field in wp_signups:

    a:8:{s:7:"field_1";s:13:"Julius Caesar";s:18:"xprofile_field_ids";s:2:"1,";s:20:"avatar_image_resized";s:84:"/serverpath/wp-content/blogs.dir/1/files/avatars/0/caesar.jpg";s:21:"avatar_image_original";s:84:"/serverpath/wp-content/blogs.dir/1/files/avatars/0/caesar.jpg";s:6:"public";s:1:"1";s:7:"lang_id";i:1;s:8:"blogname";s:9:"theempire";s:10:"blog_title";s:16:"The Roman Empire";}

    The function inserted this crap also into the wp_usermeta fields. Success! (Sort of…)

    The bit of data I need is the fullname, the second bit, in this case ‘Julius Caesar’. I’d need the second bit of data from the user with the activation key.

    How would I add that to the query?

    I’ll first try this:

    $fullname = $wpdb->get_var("SELECT meta FROM $wpdb->signups WHERE activation_key = %s", $key);
    echo $fullname;

    Nothing… :-(

    I probably had to add $key as an argument. Trying again after some food…


    peterverkooijen
    Participant

    @peterverkooijen

    With this I get four missing argument errors again. Why?

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

    $fullname = $wpdb->get_var("SELECT meta FROM $wpdb->signups WHERE activation_key = %s", $key);
    echo $fullname;

    $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', 10, 4);


    peterverkooijen
    Participant

    @peterverkooijen

    With this:

    $array = $wpdb->get_var("SELECT meta FROM $wpdb->signups");
    $my_vars = unserialize($array);

    foreach($my_vars as $name => $value)
    {
    print $name . ' has the value: ' . $value . '<br />';
    }

    I get this:

    field_1 has the value: Julius Caesar

    xprofile_field_ids has the value: 1,

    avatar_image_resized has the value: /serverpath/wp-content/blogs.dir/1/files/avatars/0/caesar.jpg

    avatar_image_original has the value: /serverpath/wp-content/blogs.dir/1/files/avatars/0/caesar.jpg

    public has the value: 1

    lang_id has the value: 1

    blogname has the value: theempire

    blog_title has the value: The Roman Empire

    So how do I get just that ‘Julius Caesar’ value?

    I’ve tried adding this and many variations. Nothing works:

    $value['field_1'] = $fullname;

    echo $fullname;


    peterverkooijen
    Participant

    @peterverkooijen

    This works!

    $array = $wpdb->get_var("SELECT meta FROM $wpdb->signups");

    $my_vars = unserialize($array);

    $fullname = $my_vars[field_1];

    echo $fullname;

    I know it’s basically DJPaul’s solution, twenty posts back, but minus the apostrophes.

    So now I know how to get the fullname, but still not how to get the fullname of the user that is activating = has the activation key?

    Why doesn’t this work then?:

    function synchro_wp_usermeta($key) {
    global $bp, $wpdb;

    $array = $wpdb->get_var("SELECT meta FROM $wpdb->signups WHERE activation_key = %s", $key);

    $my_vars = unserialize($array);

    $fullname = $my_vars[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( $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', 10, 1);


    peterverkooijen
    Participant

    @peterverkooijen

    This should work:

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

    $fullname = $meta[field_1];

    echo $fullname;

    $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', 10, 3);

    BTW, I see Lance Willet here uses $userid with wpmu_activate_user, instead of $key.

    Does $bp->loggedin_user->id even work on activation? Shouldn’t everything be based on the activation key?


    peterverkooijen
    Participant

    @peterverkooijen

    How can you identify the user on wpmu_activate_user? Only by the key from the activation link? How does that work?

    Then $bp->loggedin_user->id doesn’t work on activation either, does it?

    How is Lance Willet able to use $userid on wpmu_activate_user?

    Can I somehow use that to pull up the right meta row from wp_signups and store the values in wp_usermeta with the right user_id?

    Or can I use the email address as an identifier?


    Mike Pratt
    Participant

    @mikepratt

    @Peterverkooijen you get more flies with honey than vinegar.

    Point taken

    Just use the $user_id variable, as provided in the function argument…

Viewing 25 replies - 1 through 25 (of 29 total)
  • The topic ‘Plugin update first_name, last_name in wp_usermeta on activation’ is closed to new replies.
Skip to toolbar