Skip to content

Solution Central

WordPress, PHP & Graphic Design Tips

Skip to content
  • Blog
  • Photography
  • Web Development

Stop BuddyPress Extended Profile From Clearing WP Last Name Field

Published on September 9, 2017February 4, 2021 by Sarah

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.

BuddyPress Profile Syncing

 

 

 

 

 

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.

WordPress and BuddyPress Profiles

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.

WordPress Profile Screenshot

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!

Sarah

Sarah holds a B.S. in Information Science and worked in the corporate world as a software engineer for 12 years before striking out as a freelance WordPress web developer in 2013. When she is not furiously coding away, she can be found outdoors exploring Florida's natural beauty with a camera in hand.

Article Information

Last Modified on February 4, 2021
This entry was posted in BuddyPress, PHP, Uncategorized, WordPress
Tagged with BuddyPress, BuddyPress clears WordPress last name, BuddyPress Extended Profile, update buddypress extended profile, Update BuddyPress profile, WordPress last name field, WordPress User Profile
Bookmark this article Stop BuddyPress Extended Profile From Clearing WP Last Name Field

Post navigation

More Articles

New Dynamic User Directory WordPress Plugin
How to Build A LAMP Server
Search for:

Recent Posts

  • How to Build A LAMP Server
  • Stop BuddyPress Extended Profile From Clearing WP Last Name Field
  • New Dynamic User Directory WordPress Plugin
  • Stop WordPress Hackers in Their Tracks
  • How to Load a jQuery Plugin in WordPress

Recent Comments

    Archives

    • May 2018
    • September 2017
    • July 2016
    • October 2015
    • September 2015

    Categories

    • BuddyPress
    • jQuery
    • PHP
    • plugin
    • Security
    • Server Configuration
    • Uncategorized
    • Web Development
    • WordPress
    Copyright © 2025 SG Custom Web Solutions. All rights reserved.