Skip Navigation

[Resolved] formatting a number for display

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 0.83 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 5 replies, has 2 voices.

Last updated by Minesh 1 year, 4 months ago.

Assisted by: Minesh.

Author
Posts
#2625471

I want to display a number custom field in the format "0000" so that the field value '1' is displayed as "0001", field value '12' is displayed as "0012", etc..

#2625763

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

You can add custom shortcode and return the formatted number.

Following doc might help you:
- hidden link
- https://toolset.com/documentation/programmer-reference/adding-custom-code/how-to-create-a-custom-shortcode/

#2626631

Hi Minesh - thank you for the suggested documentation. I read through both posts and made some attempts to implement a solution, but couldn't quite match up my details with the example provided. In my case, the number field I need reformatted is the "asset number" that gets generated using another piece of custom code that I built with your assistance in reply #2622695 of a previous support ticket.

#2626649

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please share what hook you used and with what code and let me review that.

Do you want to implement the number formatting to that increment number only?

#2626669

Regrettably I've already deleted my failed attempts to write the custom code for formatting a number.

I only need to implement the formatting for the incremented 'asset number' that is generated from the following code:

/**
 * Add auto-incrementing custom field value
 */
add_action('cred_save_data', 'ts_auto_increment', 10, 2);
function ts_auto_increment($post_id, $form_data)
{
 
    if (in_array($form_data['id'], array( 1553 ))) { // Edit form ID
 
        global $wpdb;
        $values = $wpdb->get_col("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = 'wpcf-assetnumber'");
        if ( is_array( $values ) ){
            $incremented = max( $values ) + 1;
        } else {
            $incremented = 1;
        }
 
        add_post_meta( $post_id, 'wpcf-assetnumber', $incremented, true );
    }
}
#2626679

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

What if you try to use the following modified code:

/**
 * Add auto-incrementing custom field value
 */
add_action('cred_save_data', 'ts_auto_increment', 10, 2);
function ts_auto_increment($post_id, $form_data)
{
  
    if (in_array($form_data['id'], array( 1553 ))) { // Edit form ID
  
        global $wpdb;
        $values = $wpdb->get_col("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = 'wpcf-assetnumber'");
        if ( is_array( $values ) ){
            $incremented = max( $values ) + 1;
        } else {
            $incremented = 1;
        }
        $formatted_number = str_pad($incremented, 4, 0, STR_PAD_LEFT);
        add_post_meta( $post_id, 'wpcf-assetnumber', $formatted_number , true );
    }
}
#2626685

The revised code works perfectly. Thanks as ever for the awesome support.