Last active
April 16, 2021 19:05
-
-
Save kimcoleman/e8a2d2d572050d859088d73f18ee4bae to your computer and use it in GitHub Desktop.
Set Display Name on Membership Checkout and for BuddyPress Name field.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Set Display Name on Membership Checkout and for BuddyPress Name field. | |
*/ | |
function my_first_last_display_name( $user_id, $morder ) { | |
// 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 name. | |
$args = array( | |
'ID' => $user_id, | |
'display_name' => $display_name, | |
); | |
// Update WP user display name. | |
wp_update_user( $args ); | |
// Update the 'Name' xprofile Field (field ID 1). | |
if ( function_exists( 'xprofile_set_field_data' ) ) { | |
xprofile_set_field_data( 1, $user_id, $display_name ); | |
} | |
} | |
} | |
add_action( 'pmpro_after_checkout', 'my_first_last_display_name', 20, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This recipe is included in the blog post on "Set Display Name for BuddyPress Profile at Membership Checkout" at Paid Memberships Pro here: https://www.paidmembershipspro.com/set-display-name-for-buddypress-profile-at-membership-checkout/