Support

Account

Home Forums General Issues Insert Commas into ACF number field

Solved

Insert Commas into ACF number field

  • I am displaying numbers (non currency) via an advance custom field of paid_attendance – however by default number fields in ACF do not include commas or decimals etc.

    I would like for commas to be included when the numbers from paid_attendance are output to the user on the front end. I am aware this can be done through the template file but I need to make it global via functions.php. I am probably way off, but below is the code I put together. Any help would be greatly appreciated.

    // Return number with commas
    function fix_number( $wpdb ) {
    $number = get_field('paid_attendance');
    number_format($number)."<br>";
    }
    add_action('fix_number');

    I also did the best I could based on what I read in the docs and came up with the below, but again, I am assuming I am way off the mark:

    // Return number with commas
    add_action('acf/render_field_settings', 'fix_number'); 
    function fix_number( $field ) {
    $number = get_field('paid_attendance');
     number_format($number)."<br>";
    }
  • use https://www.advancedcustomfields.com/resources/acf-format_value/

    
    add_filter('acf/format_value/name=paid_attendance', 'fix_number', 20, 3);
    function fix_number($value, $post_id, $field) {
      $value = number_format($value);
      return $value;
    }
    
  • Works beautifully! Thank you so much!

    Oddly enough the only thing I was even able to get working at the template level is the below. Kept returning either 0 or “expecting integer, received string” error. Regardless, your fix did the trick globally. Much appreciated.

    // Different versions of a number
    $unformatted = get_field('paid_attendance');
    {
        $integerValue = str_replace(",", "", $unformatted);  // Remove commas from string
        $integerValue = intval($integerValue);               // Convert from string to integer
        $integerValue = number_format($integerValue);        // Apply number format
    
        $floatValue = str_replace(",", "", $unformatted);    // Remove commas from string
        $floatValue = floatval($floatValue);                 // Convert from string to float
        $floatValue = number_format($floatValue, 2);         // Apply number format
    
        echo $integerValue;                                 // Output values
    }
  • I am using a version of your code to format all the number fields:

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

    But it is also removing all the decimal places, what do I neeed to edit about this code to make it put thousand separators in but also keep the decimal places?

  • hello guys,

    this was a helpful post, but it doesnt quite solve my issue. I have two fields that I would like to add commas. This might be a silly questions, but I am an extreme novice when it comes to PHP.

    I would like to add commas to “price” and the other is “square_footage”.

    The issue I am coming across is when I put both fields “price” & “square_footage”, I get an error, but if I only enter one, it works fine. What am I missing here?

    add_filter('acf/format_value/name=price', 'fix_number', 20, 3);
    function fix_number($value, $post_id, $field) {
      $value = number_format($value);
      return $value;
    	}
    
    add_filter('acf/format_value/name=square_footage', 'fix_number', 20, 3);
    function fix_number($value, $post_id, $field) {
      $value = number_format($value);
      return $value;
    	}
  • I solved the issue… i used the same variable for both filters!

  • To keep the decimals and add (2 decimal points if there are none), you need to change
    $value = number_format($value);
    $value = number_format($value , 2);

    add_filter(‘acf/format_value/name=instrument_price’, ‘fix_number’, 20, 3);
    function fix_number($value, $post_id, $field) {
    $value = number_format($value , 2);
    return $value;
    }

    see https://www.php.net/manual/en/function.number-format.php

  • @hube2 I have always used your snippet and it has always worked great. However, now I’m running into an issue (maybe it’s a WP 5.5 thing?)

    I’m getting:

    Warning: number_format() expects parameter 1 to be float, string given in /functions.php on line 74

    Warning: Cannot modify header information – headers already sent by (output started at /functions.php:74) in /wp-includes/pluggable.php on line 1296

    Thoughts? (FYI line 74 is $value = number_format($value);)

  • Not sure why the value is a string if it’s for a number field but you can try $value = number_format(floatval($value));

  • @hube2 I just figured it out, I think… The field was a Text field when I first created it. I changed it shortly afterwards. Thanks for pointing me in the right direction!

  • @hube2 Nope, that wasn’t the cause. I deleted the field and started from scratch. Not sure… The fields are repeater sub fields. Not sure.

    Anyway, your reply worked 🤷🏼‍♂️ Thanks!

  • Can someone please share a working snippet to convert ALL number fields decimals to , ?

    Also, it would be great if we could actually choose the decimal separator, as a big part of the world uses , rather than . 🙂

  • @shambix where are you trying to replace . with ,?

    In the input field? If this is the case then the browser is supposed to use the correct character for a decimal point based on the user’s language preference. At lease this is my understanding of the way it’s supposed to work. Unfortunately, not all browsers support this. ACF is simply outputting <input type="number" />

    If you mean formatting the number on the front end then I don’t have an answer. ACF does not do any formatting for number fields. Unless you add formatting of some kind ACF returns exactly what is stored in the database.

    In both cases, nothing in ACF is setting or altering the way numbers are displayed.

  • In the backend, ACF number fields don’t allow for commas in the field, only dots, which is confusing. Hence, it would be nice to be able to set just once (or based on WP timezone/location) whether to use the metric system , or the imperial system . for decimals.

    This way in the frontend, we wouldn’t need any snippet or custom function, nor relying on a browser.

  • I am not the developer and there is a new owner of this plugin. If you contact them they might look into this.

    Please do some research on html number fields. What you’re asking in this case is not something that’s actually possible to do. I have looked into it multiple times because this is a recurring topic. The only solution that I have found is to use a text field instead of a number field and add custom JavaScript to make the text field behave like a number field including decimal point and thousands separator.

    here is one example of a related discussion.
    https://stackoverflow.com/questions/31867551/html-input-type-number-thousand-separator

    Every time I have looked into this I have found the same conclusion.

    I’m not saying that this is not a feature that is needed or wanted, what I am saying is that with the current state of the html input type number field that it is not possible at this time. In order to do so the ACF number type field would need to be completely rebuilt to ignore the HTML standard number field in favor of its own solution.

  • Can someone explain where this snippet of code goes in WP in order to get the comma inserted in the numeric field?

    add_filter(‘acf/format_value/type=number’, ‘fix_number’, 30, 3);
    function fix_number($value, $post_id, $field) {
    $value = number_format($value);
    return $value;
    }

  • In your functions.php file, or in any file that is loaded for every page load.

  • Hi,

    I think this problem has been posted above by aimkbe but I couldn’t figure out the solution.

    Below I have two numbers fields that I would like to add the comma to, when I add the first filter, it works perfectly but when I add the second it breaks the site, aimkbe says he solved the issue “I solved the issue… i used the same variable for both filters!”

    I am not sure what I need to do to make this work. Any help would be appriciated. Thank you

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

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

  • @apluskdesign You used the same function name twice

  • Thank you @zackpyle for the quick response!

    I changed the function name in the second filter and that worked!

    Allan

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

The topic ‘Insert Commas into ACF number field’ is closed to new replies.