Support

Account

Home Forums General Issues Formatting number fields using format_value

Solved

Formatting number fields using format_value

  • Was wondering if anyone could help.
    I’m formatting a lot of number fields on my site using the following code:

    add_filter('acf/format_value/name=property_latest_valuation', 'fix_number', 20, 3);
    function fix_number($value, $post_id, $field) {
      $value = number_format($value);
      return $value;
    	}

    Is there any way to use this handy bit of code to output a large number of number fields? Using an array? I’m not sure how to implement that in the code. I’m learning as I go along here.

    I’d format all the number fields, but there are a couple that don’t need the comma.

  • There are several alternatives

    If this was something I wanted to do on a large scale but did not want to have it run on all number fields I would add a custom setting to number fields https://www.advancedcustomfields.com/resources/adding-custom-settings-fields/

    Then I would create a format_value filter for all number fields that tested the new setting to see if the number should be formatted.

    Or, if there are only a few fields that should not be formatted then I would create the filter for all number fields like the one you have and I would check $field['key'] and not format those.

    Alternately, you could set up an array in your filter

    
    add_filter('acf/format_value/type=number', 'fix_number', 20, 3);
    function fix_number($value, $post_id, $field) {
      $keys = array(
        // list of field keys to format
        'field_XXXXXXX',
        'field_YYYYYYY',
        // etc...
      );
      if (in_array($field['key'], $keys)) {
        $value = number_format($value);
      }
      return $value;
    }
    
  • Perfect, thank you so much.
    I’ll try out your suggestions and go from there.

  • @hube2

    Hi there John,
    Could you help me? I’m trying to display commas in number type field, like so:
    120,000
    I tried some combinations of code I found in different threads, but nothing worked really!

    I have a particular field I want to format the numbers for, (listing_price).
    I also tried:

    add_filter('acf/format_value/number=listing_price', 'fix_number', 20, 3);
    function fix_number($value, $post_id, $field) {
      $value = number_format($value);
      return $value;
    	}

    but nothing happened.
    What do the numbers 20 and 3 mean above?
    I thought I’d need to replace them with my case for example 20000 (as currently displayed), to 20,000 as I want them displayed.
    In any case, code doesn’t work.

    Can you suggest something?

    Thank you for any help!

  • The acf/format_value filter only works on the front end for value display and is not for altering the appearance of the input field.

    Are you trying to format the input field display? Or the front end display?

  • Hi John,

    Thank you very much for kindly getting back to me!
    I just figured it out by a piece of code that works:

    add_filter('acf/format_value/name=listing_price', 'fix_number', 10, 3);
    function fix_number($value, $post_id, $field) {
    $value = number_format($value);
    return $value;
    }
    

    I wanted to format the front-end display!

    Now I just have to input the prices again of over 100 listings:-) But at least they display correctly!
    I wonder why this isn’t a default setting of ACF and a piece of code is needed to be displayed properly?

    Thank you very much for your time!

  • A number is just a number. How you format it, like 90% of ACF fields, is up to us, the developers.

  • Yes well, not everyone is a developer. You don’t have to be one to use ACF, at least that’s how I would see the future of such a plugin anyway. Otherwise, it should state….
    ‘Caution, don’t touch this unless you’re a seasoned developer;-)

    Sorry to bother you..

  • Not a bother, I was just trying to point out that it isn’t something that ACF does. It is a common misconception about ACF that it is not for developers. There are other plugins and tools that can manage the front end display or work with ACF to do this.

  • Hi All, thanks for the great info.

    I’m making a real estate website, where each property has a price that needs to be entered in the back end by the client.

    I’ve fixed the comma (thousands separator) issue on the front end display with the above snippet.

    But when using a number field (as discussed here) the client cannot use any commas when they are entering the property price, which goes against how we’ve been taught to write numbers — 1,345,000 must be entered into the ACF number field as 1345000 which doesn’t make sense to the average person.

    I know that I could create a text field instead, where a client could use commas (thousands separator), but then I’m having trouble sorting by price when that field is not a number.

    Wondering why the number field doesn’t allow for comma (or other thousands separator)?

Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.