Browse: Home / Snippets /

Set user first/last name when purchasing from PayPal account data

Contents


Snippet #

Important: All snippets are provided as-is without support or guarantees. These snippets are provided as guidelines for advanced users looking to customize LearnDash. For any additional help or support with these snippets, we recommend reaching out to a LearnDash Expert.

/**
 * Exmaples of hooking into the existing WordPress filters during the new user creation and sets the First and Last name fields 
 * from the PayPal data.
 */

/**
 * Filters user data before the record is created or updated.
 *
 * It only includes data in the wp_users table wp_user, not any user metadata.
 *
 * @since 4.9.0
 *
 * @param array    $data {
 *     Values and keys for the user.
 *
 *     @type string $user_login      The user's login. Only included if $update == false
 *     @type string $user_pass       The user's password.
 *     @type string $user_email      The user's email.
 *     @type string $user_url        The user's url.
 *     @type string $user_nicename   The user's nice name. Defaults to a URL-safe version of user's login
 *     @type string $display_name    The user's display name.
 *     @type string $user_registered MySQL timestamp describing the moment when the user registered. Defaults to
 *                                   the current UTC timestamp.
 * }
 * @param bool     $update Whether the user is being updated rather than created.
 * @param int|null $id     ID of the user to be updated, or NULL if the user is being created.
 */
add_filter( 'wp_pre_insert_user_data', function( $data = array(), $update = false, $user_id = 0 ) {
	// We only want to check new users. 
	if ( false === $update ) {

		// Next check if this from a PayPal IPN processiing event.
		if ( ( isset( $_POST['ipn_track_id'] ) ) && ( ! empty( $_POST['ipn_track_id'] ) ) ) {
			// Next check if the 'first_name' and 'last_name' post fields are present.
			if ( ( isset( $_POST['last_name'] ) ) && ( ! empty( $_POST['last_name'] ) ) && ( isset( $_POST['first_name'] ) ) && ( ! empty( $_POST['first_name'] ) ) ) {

				// Rebuild the display_name and user_nicename with better values.
				$data['display_name'] = esc_attr( $_POST['first_name'] ) . ' ' . esc_attr( $_POST['last_name'] );
				$data['user_nicename'] = $data['display_name'];
			}
		}
	}

	// Always return $data.
	return $data;
}, 1, 3 );

/**
 * Filters a user's meta values and keys immediately after the user is created or updated
 * and before any user meta is inserted or updated.
 *
 * Does not include contact methods. These are added using `wp_get_user_contact_methods( $user )`.
 *
 * @since 4.4.0
 *
 * @param array $meta {
 *     Default meta values and keys for the user.
 *
 *     @type string   $nickname             The user's nickname. Default is the user's username.
 *     @type string   $first_name           The user's first name.
 *     @type string   $last_name            The user's last name.
 *     @type string   $description          The user's description.
 *     @type bool     $rich_editing         Whether to enable the rich-editor for the user. False if not empty.
 *     @type bool     $syntax_highlighting  Whether to enable the rich code editor for the user. False if not empty.
 *     @type bool     $comment_shortcuts    Whether to enable keyboard shortcuts for the user. Default false.
 *     @type string   $admin_color          The color scheme for a user's admin screen. Default 'fresh'.
 *     @type int|bool $use_ssl              Whether to force SSL on the user's admin area. 0|false if SSL is
 *                                          not forced.
 *     @type bool     $show_admin_bar_front Whether to show the admin bar on the front end for the user.
 *                                          Default true.
 * }
 * @param WP_User $user   User object.
 * @param bool    $update Whether the user is being updated rather than created.
 */
add_filter( 'insert_user_meta', function( $meta = array(), WP_User $user, $update = false ) {

	// We only want to check new users. 
	if ( false === $update ) {

		// Next check if this from a PayPal IPN processiing event.
		if ( ( isset( $_POST['ipn_track_id'] ) ) && ( ! empty( $_POST['ipn_track_id'] ) ) ) {

			// Next check if the 'first_name' and 'last_name' post fields are present.
			if ( ( isset( $_POST['last_name'] ) ) && ( ! empty( $_POST['last_name'] ) ) && ( isset( $_POST['first_name'] ) ) && ( ! empty( $_POST['first_name'] ) ) ) {

				// Rebuild the 'nickname' with better values.
				$meta['nickname'] = esc_attr( $_POST['first_name'] ) . ' ' . esc_attr( $_POST['last_name'] );

				// Then fill in 'last_name' and 'first_name' fields. 
				$meta['last_name']  = esc_attr( $_POST['last_name'] );
				$meta['first_name'] = esc_attr( $_POST['first_name'] );
			}
		}
	}

	// Always return $meta.
	return $meta;
}, 1, 3 );