Skip Navigation

[Resolved] Shortcode Snippet Works on Custom Fields in One CPT, Does Not Work in Other CPT

This support ticket is created 2 years, 3 months ago. 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.

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

Last updated by chrisO-12 2 years, 3 months ago.

Assisted by: Minesh.

Author
Posts
#2478639

Tell us what you are trying to do?
I am using a shortcode snippet that was provided to add comma separators in numbers. I am currently using this on several number fields in one custom post type (CPT1) and it works fine. However, when I use the same shortcode on number fields on a different custom post type (CPT2), I receive a Fatal PHP error when viewing on the front end.

This is the code snippet that I use. It is placed in the Toolset, Settings, Custom Code tab.
<?php
// Put the code of your snippet below this comment.
/**
* Adds comma to number with no decimal places.
*/
toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Custom shortcode to display formatted number
function wp_format_number( $atts ) {

// Attributes
$atts = shortcode_atts(
array(
'number' => '',
),
$atts
);
return number_format($atts['number'],0,'.',',');
}
add_shortcode( 'number_0', 'wp_format_number' );

This is how it is used on a number custom field that works on CPT1.
Asking Price: $[number_0 number='[types field='asking-price' format='FIELD_VALUE'][/types]']

Here is how it is used on a number custom field that throws the PHP error on CPT2.
Cash Required: $[number_0 number='[types field='asking-price' format='FIELD_VALUE'][/types]']

Here is the first lines of the Fatal PHP error.
Fatal error: Uncaught Error: number_format(): Argument #1 ($num) must be of type float, string given
in /www/323lifeworks_849/public/wp-content/toolset-customizations/comma_decimal_number_format.php on line 21

There are 16 items listed in the Call stack: below this error.

The use as shown on CPT2 above is entered on a field on line 20 of a Content Template.

Is there any documentation that you are following?
This is the support ticket used as a guide.
https://toolset.com/forums/topic/comma-decimal-format-for-numbers-us-format/

Is there a similar example that we can see?

What is the link to your site?

This is a post that uses the Content Template for CPT1 above that does NOT result in the Fatal PHP error.
hidden link

This is a post that uses the Content Template for CPT2 above that does result in the Fatal PHP error.
hidden link

What would cause this, and how would I resolve it?

#2478877

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

What if you try to change the shortcode you added as given under:

function wp_format_number( $atts ) {

// Attributes
$atts = shortcode_atts(
array(
'number' => '0',
),
$atts
);
  if( $atts['number']=!'') {
            return number_format($atts['number'],0,'.',',');
}else{
            return "0.00";
}
}
add_shortcode( 'number_0', 'wp_format_number' );

If above solution does not help, please share problem URL where its not working as well as admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2478927

I will try this. But why does the shortcode that I use work on one content template that uses CPT1, but not on the other content template that uses CPT2?

#2478931

Minesh
Supporter

Languages: English (English )

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

Because I suspect there will be no value or empty value for the "asking-price" on second CTP post, if that is not the case I will have to review your setup.

#2478943

Hello, I entered the code provided and it changed the value of every number field to 1.

#2478947

Minesh
Supporter

Languages: English (English )

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

Can you please share problem URL and admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2479111

Hello, I am sorry for the delay.

Should I leave the snippet code that you gave me, or restore to the previous version?

Also, should I leave the number field with the Shortcode so that it shows the critical error on the page?

By the way, I retried the snippet code that you sent, and I do not get 1's in numbers fields as mentioned before. The result is the same with either snippet code with a Fatal PHP Error.

#2479141

Minesh
Supporter

Languages: English (English )

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

Yes, can you please share problem URL where I can see the error as well as admin access details and also share where you added the code I shared.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2479221

Hi Minesh,

I have to be away from my computer for 2-3 hours. Please let me know of progress and status. I will check when I return.

Thank you.

#2479225

Minesh
Supporter

Languages: English (English )

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

With the content template you shared:
=> hidden link

There was a issue with the first line:

[wpv-conditional if="( '[wpv-current-user info='user_login']' eq '[wpv-post-author format='meta' meta='user_login']' )"][

As you can see there is extra [ (bracket) at the end of the line.

I've removed that bracket and saved the content template and I can see its working as expected:
=> hidden link

Can you please confirm it works as expected.

#2479427

Prompt assistance and resolution!

My issue is resolved now. Thank you!