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;
}
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;
}
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/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/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.
get_field
so if the field name ispaid_attendance
you can take the value and format as desired, which will then apply everywhere. – WebElaine Commented Jan 31, 2019 at 19:36