Skip Navigation

[Resolved] Custom number format

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.

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 3 replies, has 2 voices.

Last updated by Minesh 2 years ago.

Assisted by: Minesh.

Author
Posts
#2532481

Tell us what you are trying to do?
I need to format a field of type number, as follows
2 digits, a hyphen, 8 digits, a hyphen, 1 digit
Example:
20-18047286-8

Is there any documentation that you are following?
https://toolset.com/forums/topic/output-format-for-a-custom-field-number/

But I can't figure out how it would be implemented in this case.

Is there a similar example that we can see?
I saw somewhere but I can't remember where, sorry!

What is the link to your site?
It's a local site

#2532903

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

What if you try to use the following modified shortcode:
- You can add the following shortcode to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

function func_show_formatted_number( $atts ) {
 
  $atts = shortcode_atts( array(
    'field' => ''
  ), $atts );

   if(ctype_digit($field) && strlen($field) == 10) {
  $number = substr($field,0,1) .'-'.
            substr($field, 1, 8) .'-'.
            substr($field, 9);
}

    return $number ;
}
 add_shortcode('show-formatted-number', 'func_show_formatted_number');

And you can use the above shortcode by adding the shortcode block or "Fields and Text" block as (if you are using Blocks):

[show-formatted-number field="[types field='your-field-slug' output='raw'][/types]"]

Where:
- replace 'your-field-slug' with your original field slug.

#2534799

I tried but it doesn't work.

I think it could be because the number is 11 digits lengh, but it's just a guess.

The number 10 in the following line, is the total lengh of digits?

if(ctype_digit($field) && strlen($field) == 10) {

#2535239

Minesh
Supporter

Languages: English (English )

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

ahh yes - you spot that right.

What if you try to use the following code:

function func_show_formatted_number( $atts ) {
  
  $atts = shortcode_atts( array(
    'field' => ''
  ), $atts );
 
   if(ctype_digit($field) && strlen($field) == 11) {
  $number = substr($field,0,1) .'-'.
            substr($field, 1, 8) .'-'.
            substr($field, 9);
}
 
    return $number ;
}
 add_shortcode('show-formatted-number', 'func_show_formatted_number');