I am trying to override the account_display_name
field within the my-account (logged in user) section which I am undertaking through two hooks. The first removes the required
attribute, and the second updates the display_name
field based on the input of first and last name.
This filter allows me to remove the required="required"
attribute from the display_name
field and works as intended:
add_filter('woocommerce_save_account_details_required_fields', 'audp_myaccount_required_fields');
function audp_myaccount_required_fields( $required_fields ) {
unset( $required_fields['account_display_name'] );
return $required_fields;
}
This next action is intended to allow me to save the submitted account_first_name
and account_last_name
as the display_name
:
add_action( 'profile_update', 'audp_myaccount_display_name', 20, 2 );
function audp_myaccount_display_name( $display_name ) {
global $current_user;
if ( isset( $_POST['account_first_name'] ) && isset( $_POST['account_last_name'] ) ) {
if ( ! empty( $_POST['account_first_name'] ) && ! empty( $_POST['account_last_name'] ) ) {
wp_update_user(
array (
'ID' => $current_user->ID,
'display_name' => sanitize_text_field( $_POST['account_first_name'] ) . ' ' . sanitize_text_field( $_POST['account_last_name'] ),
)
);
}
}
}
The problem I'm having with this code is the looping after submitting the data. The page 'eventually' returns an Internal 500 Error. If I stop the process (escape) and reload the page, the data has updated. I just don't know how to get out of this loop?
I have researched some options including remove_action( 'profile_update', 'audp_myaccount_display_name', 20, 2 )
before the wp_update_user()
and then add_action( 'profile_update', 'audp_myaccount_display_name', 20, 2 )
after wp_update_user()
but that has not seemed to work either.
Any assistance would be appreciated!
You should be able to stop endless loops by adding an additional filter, with a default false
value, that you check at the top of your function. Stopping your code from being executed when this filter returns true.
Then call that filter before you update the user meta and make it return true
. And remove it after updating the user meta. This way the check at the top will return true
the second time the functions gets called and stops the endless loop, resulting in your function only running once.
add_action( 'profile_update', 'audp_myaccount_display_name', 20, 2 );
function audp_myaccount_display_name( $display_name ) {
if ( apply_filters( 'prevent_endless_loop_updating_user_meta', false ) ) return; // prevent endless loops
global $current_user;
if ( isset( $_POST['account_first_name'] ) && isset( $_POST['account_last_name'] ) ) {
if ( ! empty( $_POST['account_first_name'] ) && ! empty( $_POST['account_last_name'] ) ) {
add_filter( 'prevent_endless_loop_updating_user_meta', '__return_true' );
wp_update_user(
array (
'ID' => $current_user->ID,
'display_name' => sanitize_text_field( $_POST['account_first_name'] ) . ' ' . sanitize_text_field( $_POST['account_last_name'] ),
)
);
remove_filter( 'prevent_endless_loop_updating_user_meta', '__return_true' );
}
}
}