Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dwanjuki/923e1c2779c296fd02374c8395d3747b to your computer and use it in GitHub Desktop.
Save dwanjuki/923e1c2779c296fd02374c8395d3747b to your computer and use it in GitHub Desktop.
Set Display Name on Membership Checkout and for BuddyPress Nickname field.
<?php
/**
* Set Display Name on Membership Checkout and for BuddyPress Nickname field.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_first_last_display_name( $user_id ) {
// Get user's first and last name.
$first_name = get_user_meta( $user_id, 'first_name', true );
$last_name = get_user_meta( $user_id, 'last_name', true );
if ( ! empty( $first_name ) && ! empty( $last_name ) ) {
$display_name = trim( $first_name . ' ' . $last_name );
} elseif ( ! empty( $first_name ) ) {
$display_name = trim( $first_name );
}
if ( ! empty( $display_name ) ) {
// Should set "display_name" as well as the BuddyPress Profile field Nickname.
$args = array(
'ID' => $user_id,
'display_name' => $display_name,
);
// Update WP user display name.
wp_update_user( $args );
// Update the 'Nickname' xprofile Field (field ID 3).
if ( function_exists( 'xprofile_set_field_data' ) ) {
xprofile_set_field_data( 3, $user_id, $display_name );
}
}
}
add_action( 'pmpro_after_checkout', 'my_first_last_display_name', 20 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment