Eagle-eyed website admins will notice an annoying problem that can occur when users update their BuddyPress Extended profiles — doing so may inadvertently clear out the Last Name field of their WordPress profiles. This can disrupt plugins that rely upon the WP last name field, such as my own Dynamic User Directory plugin. Users without a last name in their WordPress profile won’t show up in the user directory.
What causes the BuddyPress Extended Profile to behave this way, and how do you fix the issue? First let’s take a look at the configuration in which it arises.
The Preconditions
1. BuddyPress Profile Syncing is turned on. This is the default setting.
2. The user was already registered in WordPress before BuddyPress was installed. In this event, BuddyPress Extended Profile inserts only the first name into its required “Name” field, when it should actually contain both the first AND last name.
The Problem Scenario
1. The user updates some field on their extended profile, but does not update the required “Name” field to supply a last name.
2. Because profile syncing is turned on, BuddyPress attempts to update the WP profile’s first and last name fields with the contents of the BP Name field.
3. The first name field syncs fine, but the last name is missing on the BP profile. BP mistakenly updates the WP last name field with an empty string.
The Solution
To solve this issue I’ve written two little filters in PHP. The first filter will capture the WordPress last name field and stick it in a temporary user meta field behind the scenes before BuddyPress can clear it out. The second filter will restore the WordPress last name field after it has been cleared out by BuddyPress, but only if the BuddyPress name field does not have a last name when it is submitted. The code looks like this:
/** * Filter that captures the last name WP profile field before it can be cleared out & sticks it in a temporary meta fld. * This action gets executed just before the BP profile sync. * **/ function action_xprofile_updated_profile_b4_sync() { global $current_user; $last_name = get_user_meta($current_user->id, 'last_name', true); //Stick the WP last name fld in a temp meta fld before BP can clear it update_user_meta($current_user->id, 'last_name_tmp', $last_name ); } add_action('xprofile_updated_profile', 'action_xprofile_updated_profile_b4_sync', 1); /** * Filter that grabs the last name from the temp meta fld and updates the WP last name fld after it is cleared out by BP * This action gets executed just after the BP profile sync via the priority of '9999'. * **/ function action_xprofile_updated_profile() { global $current_user; //Get the WP last name that was stored in the temp meta fld prior to the profile sync $last_name = get_user_meta($current_user->id, 'last_name_tmp', true); //Get the WP last name field now that BP has modified it $updated_last_name = get_user_meta($current_user->id, 'last_name', true); //Remove the temp field delete_user_meta($current_user->id, 'last_name_tmp'); //Do nothing if BP updated the WP profile with a new last name if(!empty($updated_last_name) && $last_name !== $updated_last_name) return; else //Restore the WP last name fld if empty update_user_meta( $current_user->id, 'last_name', $last_name ); } add_action('xprofile_updated_profile', 'action_xprofile_updated_profile', 9999);
Simply copy the above code and paste it into your theme’s functions.php file. Note carefully that this code only executes when users update their own BuddyPress Extended Profiles. It will not execute when an admin updates someone else’s BuddyPress profile. That said, these filters should effectively eliminate the nagging issue of the disappearing last name field in the WordPress profile.
Did this post help you out? Please say thanks by clicking the Facebook “like” button below!