Skip to content

Instantly share code, notes, and snippets.

@MaximilianoRicoTabo
Created September 23, 2024 22:04
Show Gist options
  • Save MaximilianoRicoTabo/a0ed6e650770a2ffb305757c02e9c5fe to your computer and use it in GitHub Desktop.
Save MaximilianoRicoTabo/a0ed6e650770a2ffb305757c02e9c5fe to your computer and use it in GitHub Desktop.
sync billing phone with a custom user field called pmpro_custom_phone
<?php
/**
* This recipe bring Order billing info phone number into profile page.
*
* 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/
*/
//hook into pmpro_show_user_profile action create a custom field and save into user meta
add_action( 'pmpro_show_user_profile', 'pmprocustom_show_user_profile');
/**
* pmpro_show_user_profile action callback
*
* @param WP_User $user
* return void
*/
function pmprocustom_show_user_profile( $user ) {
//Check if we already synced. Get field from user meta
$pmpro_custom_phone = get_user_meta( $user->ID, 'pmpro_custom_phone', true );
//We didn't let's get it from the last order
$last_order = new MemberOrder();
$last_order->getLastMemberOrder( $user->ID, array('success') );
$phone = $last_order->billing->phone;
//If we still don't have a phone number let's use the one from last order.
if( ! $pmpro_custom_phone ) {
$pmpro_custom_phone = $phone;
}
?>
<div class="pmpro_form_field">
<label class="pmpro_form_label" for="pmpro_custom_phone">Phone</label>
<input type="text" id="pmpro_custom_phone" name="pmpro_custom_phone" value="<?php echo esc_attr( $pmpro_custom_phone ); ?>" class="pmpro_form_input pmpro_form_input-text" />
</div>
<?php
}
//hook into pmpro_personal_options_update action to save the custom field
add_action( 'pmpro_personal_options_update', 'pmprocustom_save_user_profile');
/**
* Update the user meta with the custom phone field
*
* @param int $user_id The user ID
* return void
*/
function pmprocustom_save_user_profile( $user_id ) {
update_user_meta($user_id, 'pmpro_custom_phone', $_POST['pmpro_custom_phone']);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment