• Resolved jonoman1

    (@jonoman1)


    I have a number field on my form, but when the user enters a “0” it does not show up in their profile. I know UM is working on a fix for this for another update, but is there a code snippet I can use for now to get 0’s to show up as not blank? I tried the code on the below support thread but it didn’t work for me. I’m trying this on a number field I’m using a percentages. The user can add 0-100 for the field, and 0 is a fairly common answer.

    https://wordpress.org/support/topic/dropdown-with-default-value-0-hidden-on-profile-page/

Viewing 8 replies - 1 through 8 (of 8 total)
  • @jonoman1

    Replace the first line in your code snippet with this code for a number field:

    add_action("um_view_field_value_number", function( $result, $data ){

    Thread Starter jonoman1

    (@jonoman1)

    Thanks, I tried this and it does save the “0” entry on the edit side now, but it still doesn’t show that field at all when viewing the profile page. Is there something else missing from the code?

    @jonoman1

    You can try this code snippet without your meta-key and all number fields are now displaying 0:

    add_action("um_view_field_value_number", function( $result, $data ){
    
        if( 0 == $result || '0' == trim( $result ) || empty( $result )) {
    
            return "0 ";  // Extra space required
        }
        return $result;
    },10, 2);
    Thread Starter jonoman1

    (@jonoman1)

    Thank you! That seems to work in the profile area. It’s not showing up in the member directory on the profile card for fields with “0” so I wonder if this pulls from the metakey differently. If I wanted to put a “%” after the number would I put it into this code as well?

    • This reply was modified 3 years, 8 months ago by jonoman1.

    @jonoman1

    You need another code snippet for the Members Directory.
    Where and how are you using this number field in the Members Directory?

    @jonoman1

    You can now replace the first code snippet and try this code snippet, where UM Members Directory will show “0” numbers and all numbers will have a % sign too.

    add_filter( "um_ajax_get_members_data", "um_profile_number_members_data", 10, 3 );
    
    function um_profile_number_members_data( $data_array, $user_id, $directory_data ) {
    
        foreach ( UM()->builtin()->saved_fields as $key => $data ) {
            if( $data['type'] == 'number' ) {
    
                if ( in_array( $key, $directory_data['reveal_fields']) || 
                     in_array( $key, $directory_data['tagline_fields']) ||
                     in_array( $key, maybe_unserialize( $directory_data['search_fields'] )) || 
                     in_array( $key, maybe_unserialize( $directory_data['sorting_fields'] ))) {
    
                    if( !isset( $data_array[$key] )) {
                        $data_array[$key] = '0'; 
                        $data_array['label_' . $key] = UM()->fields()->get_label( $key );
                    } 
                    $data_array[$key] .= '%';
                }
            }
        }
    
        return $data_array;
    }
    
    add_action( "um_view_field_value_number", function( $result, $data ) {
    
        if( 0 == $result || '0' == trim( $result ) || empty( $result )) {
            $result = "0"; 
        }
        return $result . '%';
    },10, 2);
    Thread Starter jonoman1

    (@jonoman1)

    Great support! That works perfectly! Thank you so much for your time and expertise.

    @jonoman1

    Thanks for verifying the solution and making the thread Resolved.

    I have added this code snippet to the other rather old GitHub issue about displaying “0” values for select fields.

    https://github.com/ultimatemember/ultimatemember/issues/747

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘How do I get a number field with value of “0” to show’ is closed to new replies.