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..
Minesh
Supporter
Les langues:
Anglais (English )
Fuseau horaire:
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:
- lien caché
- https://toolset.com/documentation/programmer-reference/adding-custom-code/how-to-create-a-custom-shortcode/
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.
Minesh
Supporter
Les langues:
Anglais (English )
Fuseau horaire:
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?
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 );
}
}
Minesh
Supporter
Les langues:
Anglais (English )
Fuseau horaire:
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 );
}
}
The revised code works perfectly. Thanks as ever for the awesome support.