1

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');

EDIT Below is the fix and I have now got it working. Clearly and as Rick mentioned I was not using hooks properly. After reading deeper into the ACF docs and consulting other users on their forum, I was able to do what I need.

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;
}
1
  • I would try ACF's support channels as they can tell you how best to format globally. I believe you can add a filter to get_field so if the field name is paid_attendance you can take the value and format as desired, which will then apply everywhere.
    – WebElaine
    Commented Jan 31, 2019 at 19:36

1 Answer 1

2

You need to 'hook' your function to a valid hook. Your add_action call is not doing anything, because it is not a proper call to a hook.. See the docs https://developer.wordpress.org/reference/functions/add_action/ for add_action.

Your function is the second parameter of the add_action hook. The first parameter is 'where' to hook into WordPress. If you want to modify the post content, then hook into the_content (see https://developer.wordpress.org/reference/hooks/the_content/ ).

But, then you need to parse the_content to find a number to format. The the_content hook works on the entire post (or page) content, so your function won't work.

So, not sure that you can do what you want. But if you are going to use hooks, then you need to learn how they work. The above links are a starting point.

1
  • Thanks for your help and patience. Clearly I have a lot of learning to do. I made an edit to my original post with the fix that has worked for me. Appreciate your time and I am going to dig into the links you posted.
    – James
    Commented Feb 1, 2019 at 4:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.