Skip to:
Content
Pages
Categories
Search
Top
Bottom

Make 2+ part name in full name required + xprofile_sync_wp_profile()


  • peterverkooijen
    Participant

    @peterverkooijen

    The cleanest way to get separate firstname and lastname into the database seems to be to

    1. make a two or more part name in fullname on the registration form required

    2. consistently split the input from Buddypress’ fullname field and synchronize with usermeta using xprofile_sync_wp_profile()

    How would I do that? I assume this could be turned into a plugin?:

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

    if ( (int)get_site_option( 'bp-disable-profile-sync' ) )
    return true;

    $fullname = xprofile_get_field_data( BP_XPROFILE_FULLNAME_FIELD_NAME, $bp->loggedin_user->id );
    $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( 'xprofile_updated_profile', 'xprofile_sync_wp_profile' );

    Hook it to register_post? Does this code already check if there’s a two part name? I suspect that happens at ‘if ( false === $space)…etc.’, but I don’t see an error message + redirect or something like that.

    Or is it impossible to use this code during register_post because of this line:

    $bp->loggedin_user->id

    How can I modify the code to make it work during registration? Can I just remove that line? Or run this function the first time the new user logs in? Is there a hook on_activation or something like that?

    Has anyone used xprofile_sync_wp_profile()? Can anyone share code examples? This would solve a major structural problem in Buddypress imho.

    I need to come up with a solution, because integration with most mailing lists depends on having firstname and lastname stored somewhere.

    Alternatively I can use fullname for firstname and create a second custom xprofile field for lastname, but I suspect that solution could cause lots of problems further down the line.

Viewing 12 replies - 1 through 12 (of 12 total)

  • peterverkooijen
    Participant

    @peterverkooijen

    Is the hook to use this?:

    user_register

    Runs when a user’s profile is first created. Action function argument: user ID.

    So something like this?:

    <?php
    /*
    Plugin Name: Real Name Synchro
    Plugin URI: http://
    Version: v0.001
    Author: peterverkooijen
    Description: A plugin to store firstname and lastname from the fullname field in Buddypress registration in WPMU usermeta tables, based on examples in the <a href="http://www.devlounge.net">Devlounge</a> series.
    */
    if (!class_exists("RealNameSynchro")) {
    class RealNameSynchro {
    function RealNameSynchro() { //constructor

    }
    function xprofile_sync_wp_profile() {
    global $bp, $wpdb;

    if ( (int)get_site_option( 'bp-disable-profile-sync' ) )
    return true;

    $fullname = xprofile_get_field_data( BP_XPROFILE_FULLNAME_FIELD_NAME, $bp->loggedin_user->id );
    $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( 'xprofile_updated_profile', 'xprofile_sync_wp_profile' );

    }

    } //End Class RealNameSynchro

    if (class_exists("RealNameSynchro")) {
    $dl_pluginSeries = new RealNameSynchro();
    }
    //Actions and Filters
    if (isset($dl_pluginSeries)) {
    //Actions
    add_action('user_register', array(&$dl_pluginSeries, 'xprofile_sync_wp_profile'), 1);
    //Filters
    }

    ?>

    Testing it now…

    Edit: Activating this plugin produces a white screen. Not a good start… :-(

    Where did I go wrong?


    peterverkooijen
    Participant

    @peterverkooijen

    I had to remove this line of course:

    add_action( 'xprofile_updated_profile', 'xprofile_sync_wp_profile' );

    Plugin now activated. Now testing to see if it does anything…

    Edit: The plugin does not derail registration, but doesn’t do anything useful either. The first_name and last_name do not show up in wp_usermeta. :-(

    Why doesn’t it work? Any suggestions very much appreciated.


    peterverkooijen
    Participant

    @peterverkooijen

    Or can I just add this to bp_custom.php?

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

    if ( (int)get_site_option( 'bp-disable-profile-sync' ) )
    return true;

    $fullname = xprofile_get_field_data( BP_XPROFILE_FULLNAME_FIELD_NAME, $bp->loggedin_user->id );
    $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', 'xprofile_sync_wp_profile' );

    Note the user_register edit in add_action.

    Testing it now…

    Edit: Apparently the function xprofile_sync_wp_profile() is already included in bp-xprofile/bp-xprofile-filters.php…

    Trying that with the same edit:

    add_action( 'user_register', 'xprofile_sync_wp_profile' );

    Edit: Still no separate first_name and last_name in wp_usermeta. :-(

    This is driving me nuts. Can anyone please help? Getting a first and last name in the database should not be this hard for a social network!


    peterverkooijen
    Participant

    @peterverkooijen

    Another attempt! What I really need is a variation of the xprofile_sync_wp_profile function that takes $fullname directly from the registration form and can be executed on user_register.

    That shouldn’t be too hard, right?

    function synchro_wp_usermeta() {
    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( 'user_register', 'synchro_wp_usermeta' );

    I have another plugin that also takes data from the registration form. That plugin has this in the code: $input_data. Is that standard wp? There’s nothing about it in the Codex.

    That other plugin has this function, minus irrelevant bits:

    function subscribe($input_data) {

    // $post_data = array();
    foreach ($input_data as $varname => $varvalue) {
    $post_data[$varname] = $varvalue;
    }

    }
    $post_data='action=subscribe&group_ids[]='.$this->lid.'&email_address='.$this->email_id.'&firstname='.$this->name_id;

    So in my case that last line would become something like this?:

    $post_data= 'first_name='.$this->first_name;

    Of course somehow put together in one function. Like this?:

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

    $post_data = array();
    foreach ($input_data as $varname => $varvalue) {
    $post_data[$varname] = $varvalue;
    }

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

    Can anyone please help? What should I put here for the “fullname” field?:

    $fullname = $post_data[fullname];

    Or is the entire approach wrong?


    peterverkooijen
    Participant

    @peterverkooijen

    Here is a plugin that “forces users to provide first and last name upon registration” and stores them in wp_usermeta.

    Would this plugin be compatible with Buddypress? Looking into it now…


    r-a-y
    Keymaster

    @r-a-y

    I’m guessing it wouldn’t since WP vs. WPMU are different beasts, especially when it comes to registration.


    peterverkooijen
    Participant

    @peterverkooijen

    You’re right r-a-y. Tried it. It had no effect on Buddypress’ registration form.

    Back to my previous attempt; I’m stuck at intercepting fullname input from the Buddypress registration form. Do you know how?

    Based on code in another plugin I put together something based on ‘$input_data’:

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

    $post_data = array();
    foreach ($input_data as $varname => $varvalue) {
    $post_data[$varname] = $varvalue;
    }

    $fullname = $post_data[fullname];
    ...

    But I don’t know if that $input_data is a standard wp tag or something else. Also the structure of the xprofile tables is very different from the structure of the regular wp users tables, so I’m not sure if that ‘$input_data as $varname => $varvalue’ is applicable.

    Where is the code that processes data input from the Buddypress registration form? I’d like to use that as example, but have no clue where it is.


    peterverkooijen
    Participant

    @peterverkooijen

    I think the code that actually processes the input from the registration form is in bp_xprofile/bp-xprofile-classes.php, Class BP_XProfile_ProfileData etc.

    Input data is apparently stored with function save() and the input data is a combination of field_id + value for the same $this->id.

    In the html ‘* Name’ is an optional field with the name/id field_1. I guess that’s the field_id?

    Now I need to figure out how to get a definition of $fullname out of that. I’ll look for more examples/tutorials how processing form input in php actually works…


    peterverkooijen
    Participant

    @peterverkooijen

    bp-xprofile-signup.php has a promising looking function:

    xprofile_on_activate_user()

    When a user activates their account, move the extra field data to the correct tables.

    function xprofile_extract_signup_meta( $user_id, $meta ) {
    // 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();

    $field->save();
    }

    update_usermeta( $user_id, 'last_activity', time() );
    }

    Does $field->save(); have something to do with function save()?

    function update_usermeta actually updates wp_usermeta. So why is field_1 not stored in wp_usermeta?! Very confused…


    peterverkooijen
    Participant

    @peterverkooijen

    Would this work? Does it make sense?

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

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

    I’m looking for a way to define $fullname, to take the input from field_1 (* Name).

    Please, anyone, help. I’ve been struggling with this for about four months now.

    EDIT: Tried the code in bp_custom.php. It produced (?) a nasty database error, may have even crashed the server… :-(


    razzgpe
    Participant

    @razzgpe

    Hey! I know, its an old topic.

    But did you get a solution?
    I have the same problem. I want to have two fields for first- and lastname instead of fullname (field_1).

    Best regards,
    Razz!


    Suzan Jasmin O.
    Participant

    @oscar582234

    Hi
    I have the latest Wp and latest BuddyPress 1.9.2
    On the new user registration form it has FULL NAME box
    But I need FIRST NAME and LAST NAME boxes. So I can in admin panel and users on frontend can register new users can fill their names with 2 boxes. And on search page users can search other users by first name or last name. And I can hide last names for some users etc.. Its always convenient to have first and last names in separate DB columns. So please help me out, how can I have the BuddyPress plugged site of mine to function like above?
    Thanks

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Make 2+ part name in full name required + xprofile_sync_wp_profile()’ is closed to new replies.
Skip to toolbar