Forked from kimcoleman/my_first_last_display_name.php
Last active
April 16, 2021 06:15
-
-
Save andrewlimaza/ae8ac99d9368119ea2cd9d8cafd77f4a to your computer and use it in GitHub Desktop.
Set Display Name on Membership Checkout and for BuddyPress Name field.
This file contains 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 ); | |
if ( ! 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