Adding thousands separator and decimals with a filter - my code not working
- This topic has 5 replies, 2 voices, and was last updated 1 year, 10 months ago by
Peter.
-
AuthorPosts
-
June 19, 2023 at 9:15 PM #42278
OldGreg
ParticipantI have this code:
<?php add_filter( 'rwmb_meta', function( $value, $field_id, $args, $object_id ) { if ( $field_id === 'pricing_dollars' ) { $value = number_format((float)$value, 3, '.', ''); // Outputs -> 0.100 } return $value; }, 10, 4 );
Expected result is that a number stored as 0.15 becomes 0.150 on the front end. It doesn't work when the field "pricing_dollars" is used. Can you please provide a code snippet example of how to do number formatting, and also how to add a thousand separator? I see many support posts using sanitizing and callbacks to store the value as a text field. We don't want that - we just want to control the way it appears on front end. I have see your baseline documentation for rwmb_meta, but I can't see to make it work.
Thank you for your help.
June 20, 2023 at 8:58 PM #42295Peter
ModeratorHello,
You can try to use the field type
multimask
to display the price by using this plugin https://github.com/badabingbreda/field-text-multimaskRefer to this topic https://support.metabox.io/topic/thousand-separator
June 21, 2023 at 9:20 AM #42304OldGreg
ParticipantI don’t want to use a plug-in, I want to use a PHO filter function. Can you please detail the answer using the pho function/filter?
June 22, 2023 at 9:32 PM #42324Peter
ModeratorHello,
The number field only accepts the number value, without the format. You should format the number when outputting the field value, for example:
$number = rwmb_meta( 'number' ); $number = number_format( $number, 3, '.', ',' ); $number = rtrim( $number, '0.' ); echo $number;
Refer to this topic https://stackoverflow.com/questions/14531679/remove-useless-zero-digits-from-decimals-in-php
June 22, 2023 at 9:40 PM #42325OldGreg
ParticipantPeter, close! I am not asking for the php on how to display the field. This is ECHO, what I need is the PHP code for something like WPCodebox to use that will let me manipulate a metabox field when it is called for display on the front end by some other plugin or page builder. The wirteup on the rwmb_meta filter doesnt work, so I need a specific code block that you know works and I can work backward from there. Can you show me how you would apply what you said above, but in a filter php block to apply to anywhere the field is called?
June 23, 2023 at 10:57 PM #42337Peter
ModeratorHello,
Do you try to combine the code? Replacing the
echo
state withreturn
? For example:add_filter( 'rwmb_meta', function( $value, $field_id, $args, $object_id ) { if ( $field_id == 'number' ) { $value = number_format( $value, 3, '.', ',' ); $value = rtrim( $value, '0.' ); } return $value; }, 10, 4 );
Also, this filter will work when you call the function
rwmb_meta()
to display the value on the frontend. It will not work with other page builders if they are getting field value from the database.So I don't think there is a difference between using the code to
echo
field value on the frontend with the filter. -
AuthorPosts
- You must be logged in to reply to this topic.