Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

WordPress calling profile_update on User Registration Errors to put of memory

I’m trying to create a custom User Registration form on a WordPress theme. I made a plugin where I’ve added my desired fields and save those fields with some hooks.

All of it works fine except for when I try to include the profile_update hook, then I get a out of memory error. I assume something ain’t right here so here’s the code

    add_action('woocommerce_register_form_start', 'front_end_field');

function front_end_field()
{
?>
    <p class="form-row form-row-first">
        <label for="billing_first_name"><?php _e('First name', 'text_domain'); ?><span class="required">*</span></label>
        <input required type="text" class="input-text" name="billing_first_name" id="billing_first_name" value="<?php if (!empty($_POST['billing_first_name'])) esc_attr_e($_POST['billing_first_name']); ?>" />
    </p>
....

add_action('woocommerce_register_post', 'validate_input', 3, 10);

function validate_input($username, $email, $validation_errors)
{
    if (isset($_POST['billing_first_name']) && empty($_POST['billing_first_name'])) {
        $validation_errors->add('billing_first_name_error', __('<strong>Error</strong>: First name is required!', 'text_domain'));
    }
.....


function save_data($customer_id)
{
    if (isset($_POST['billing_first_name'])) {
        update_user_meta($customer_id, 'first_name', sanitize_text_field($_POST['billing_first_name']));
        update_user_meta($customer_id, 'billing_first_name', sanitize_text_field($_POST['billing_first_name']));
... 


// Error starts below 
    add_action( 'profile_update', 'my_profile_update', 10, 2 ); // Causes Site to Crash (Max out memeory)

function my_profile_update( $user_id, $old_user_data ) {

    if (isset($_POST['user_url'])) {
        wp_update_user( array( 'ID' => $user_id, 'user_url' =>  sanitize_text_field($_POST['user_url']) ) );
    }

}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

profile_update hook is triggered again when you update the user causing an infinite loop, you can prevent this by removing the hook before updating like this

add_action( 'profile_update', 'my_profile_update', 10, 2 ); // Causes Site to Crash (Max out memeory)

function my_profile_update( $user_id, $old_user_data ) {
    remove_action( 'profile_update', 'my_profile_update', 10 );
    if (isset($_POST['user_url'])) {
        wp_update_user( array( 'ID' => $user_id, 'user_url' =>  sanitize_text_field($_POST['user_url']) ) );
    }
    add_action( 'profile_update', 'my_profile_update', 10, 2 );

}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading