Skip Navigation

[Résolu] Add Leading Zeros To Number Field

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem: I would like to display a number from a custom field in my post with at least 3 digits. If necessary, I would like to pad the number with leading zeros.

Solution: Display the custom field using a custom shortcode. This shortcode will append leading zeros if necessary to the display without affecting the value stored in the database. This is helpful because it can be used in various places on the site and it does not break numeric sorting, calculations, or comparisons.

/* --------------------------------------------- */
// CUSTOM THREE DIGIT SHORTCODE
add_shortcode( 'pad_three_digits', 'my_pad_three_digits_func');
function my_pad_three_digits_func($atts)
{
$num = $atts['num'];
return str_pad($num, 3, '0', STR_PAD_LEFT);
}
[pad_three_digits num="1"]    // 001
[pad_three_digits num="10"]   // 010
[pad_three_digits num="100"]  // 100
[pad_three_digits num="1000"] // 1000
[pad_three_digits num="[types field='project-number' format='FIELD_VALUE'][/types]"] // outputs a custom field number padded if necessary

Relevant Documentation: https://codex.wordpress.org/Shortcode_API
https://toolset.com/documentation/customizing-sites-using-php/functions/

This support ticket is created Il y a 6 années et 11 mois. There's a good chance that you are reading advice that it now obsolete.

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 6 réponses, has 2 voix.

Last updated by Team DNK Il y a 6 années et 11 mois.

Assisted by: Christian Cox.

Auteur
Publications
#515431

Hello Support... I believe I got this working with a little help from this form and Google searches.

1. I would value your input to confirm the below code is properly written for adding Leading Zeros to achieve three digit custom number fields?

2. How would I rewrite this code for a single field not an array of fields?

$field_slugs = array(
    'project-number',
    'task-number',
    'purchase-number',
    'invoice-number',
);
foreach($field_slugs as $field_slug){
    add_filter( 'wpcf_fields_slug_' . $field_slug . '_value_get', 'leading_zeros');
    add_filter( 'wpcf_fields_slug_' . $field_slug . '_value_display', 'leading_zeros');
}
function leading_zeros($value){
    return str_pad($value, 3, '0', STR_PAD_LEFT);
}

Kind Regards,
Dave

#515493

Hi, I'm not sure I understand exactly what you're trying to accomplish so I'm a bit confused. Could you help clarify for me?

- What is the end goal of this code?
- Where are you placing this code, and when does it execute?
- Should the values of these fields be permanently modified in the database to include leading zeros, or do you simply want to change the way they are displayed?
- If you just want to change one field, what is the slug of that field?

#515506
Leading zeros on number field.png

Hi Christian... I attached a screenshot and provided further explanation below.

- What is the end goal of this code?
I am building a project management system for my company and will be using number fields that will be auto numbered starting at 1. I want to have the numbers display as three digit numbers but include leading zeros until all three digit places are used. See screenshot of a view that queries project post that are being auto numbered.

-- Where are you placing this code, and when does it execute?
I am placing the code in the functions file and it is intended to format the custom number fields in the array.

- Should the values of these fields be permanently modified in the database to include leading zeros, or do you simply want to change the way they are displayed?
That is a great question and I believe what I am currently doing is only changing the output. Would prefer to know how to properly do it both ways as I could see needing this for different applications.

- If you just want to change one field, what is the slug of that field?
Lets just say it's the first one from my example, project-number.

Kind Regards,
Dave

#515525

Option 1: This is how you would accomplish the display-only modification.

I think you're on the right track, but I would make this a bit easier to use by creating a custom shortcode that pads any string to be at least 3 digits with leading zeros. This will make it easy to use anywhere on your site. Add this code to functions.php:

/* --------------------------------------------- */
// CUSTOM THREE DIGIT SHORTCODE
add_shortcode( 'pad_three_digits', 'my_pad_three_digits_func');
function my_pad_three_digits_func($atts)
{
$num = $atts['num'];
return str_pad($num, 3, '0', STR_PAD_LEFT);
}

You can see that we will pass in an attribute "num" with a value that we will then pad out if necessary and return. Example tests:

[pad_three_digits num="1"]    // 001
[pad_three_digits num="10"]   // 010
[pad_three_digits num="100"]  // 100
[pad_three_digits num="1000"] // 1000

If you want to use the shortcode in a post or loop, you can do something like this:

[pad_three_digits num="[types field='project-number' format='FIELD_VALUE'][/types]"]

Option 2: The permanent, changed in the database modification approach depends on how your posts are created and when you want to make the modification. For a post created by CRED, you should utilize a cred_before_save_data hook. This will modify the data after it is collected by CRED, but before it's saved in the database. For a post created in wp-admin, you would need to utilize a save_post hook.

In both of those approaches, you would just modify the post meta data directly before storing it in the database.

#515532

Thank You Christian... Super helpful and I believe I fully understand Option 1 which makes a lot of sense.

As for Option 2 I need to play with it, want to be sure I am understanding things correctly. Hope to be setting up some CRED forms later today or tomorrow on my beta site and give this a go. It would be nice to have the numbering consistent all the way down to the database table cell.

#515541

Sounds good, keep me posted here. I am off Friday and Saturday but I return Sunday.

#515558

Many Thanks... I am going to use the short code method for my Project and document numbering need given it works better with Auto Numbering as per the other Forum Thread you helped me with. For the benefit of others I am providing a link to the other thread below.

Also appreciate the additional info regarding the difference between creating and modifying posts via CRED and the WordPress admin.

https://toolset.com/forums/topic/disable-cpt-auto-numbering-when-editing-post/

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.