Skip Navigation

[Resolved] 0 (zero) values will not save

This thread is resolved. Here is a description of the problem and solution.

Problem:

A custom Toolset user field (wpcf-number-of-bookings) was initialized with a value of 0 when a user was created. However, when editing and saving the user from WordPress Dashboard → Edit User, the value 0 was cleared and saved as empty, even when the field type was set as a single-line text or number field. This behavior occurred consistently across multiple sites.

Solution:

A PHP hook was added to listen to the profile_update action. After the user profile is saved, the code checks whether the custom user meta field is empty or null. If so, it explicitly resets the value back to "0" using update_user_meta(). This ensures that zero values persist after saving the user profile in the WordPress admin.

Relevant Documentation:

https://toolset.com/documentation/customizing-sites-using-php/creating-custom-user-profiles/

https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 1 reply, has 1 voice.

Last updated by terryE 1 month, 3 weeks ago.

Assisted by: Christopher Amirian.

Author
Posts
#2839903

We have a custom user field that is set to 0 when a user is created

update_user_meta( $user_id, 'wpcf-number-of-bookings', "0" );

The field is single line text field, but also tried Number field.

When the use id edited, it displays 0.

When the user is edited (Dashboard > edit user) and saved, the 0 value is set to nothing when you user the user.

Have this issue on a few sites. Just wondered the best way to way around this.

Thanks guys!

#2839989

Christopher Amirian
Supporter

Languages: English (English )

Hi,

Welcome to Toolset support. Toolset has an empty database storage when it comes to the checkboxes but I am not sure about numbers.

You can simply add the PHP code below:

add_action('profile_update', function($user_id) {
    $key = 'wpcf-number-of-bookings';

    // If Toolset/WordPress cleared it on save, set it back to "0"
    $val = get_user_meta($user_id, $key, true);
    if ($val === '' || $val === null) {
        update_user_meta($user_id, $key, '0');
    }
}, 10, 1);

To know where to add it:
https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

This should force 0.

For more information about user meta fields:
https://toolset.com/documentation/customizing-sites-using-php/creating-custom-user-profiles

Thanks,

#2839993

Perfect, thank you!