Adding thousands separator and decimals with a filter - my code not working

Support General Adding thousands separator and decimals with a filter - my code not working

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #42278
    OldGregOldGreg
    Participant

    I 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.

    #42295
    PeterPeter
    Moderator

    Hello,

    You can try to use the field type multimask to display the price by using this plugin https://github.com/badabingbreda/field-text-multimask

    Refer to this topic https://support.metabox.io/topic/thousand-separator

    #42304
    OldGregOldGreg
    Participant

    I 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?

    #42324
    PeterPeter
    Moderator

    Hello,

    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

    #42325
    OldGregOldGreg
    Participant

    Peter, 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?

    #42337
    PeterPeter
    Moderator

    Hello,

    Do you try to combine the code? Replacing the echo state with return? 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.

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.