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
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?
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
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.
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.
Sounds good, keep me posted here. I am off Friday and Saturday but I return Sunday.
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/