Skip Navigation

[Resolved] Calculation shortcode issue

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Waqar 6 months, 3 weeks ago.

Assisted by: Waqar.

Author
Posts
#2694349
Screenshot 2024-04-25 at 10.57.22.png

Tell us what you are trying to do?

I'm trying to calculate some fields using a shortcode. The shortcode works, but when I try to edit the Tooset template via the frontend, I get a syntax error. The shortcode works correctly until I edit the Toolset template. I've inserted the shortcode using the WP Snippets plugin.

Is there any documentation that you are following?
https://toolset.com/forums/topic/displaying-a-weight-as-pounds-at-3-and-above-and-ounces-below-3/

Is there a similar example that we can see?

What is the link to your site?

#2694580

Hi,

Thank you for contacting us and I'd be happy to assist.

Have you tried including the same code snippet through some other method? For example, using Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file?

If the issue persists through other methods, too, I'll need to see how this template and the custom shortcode are being used in the admin area. You can share temporary admin login details, in reply to this message.

Note: Your next reply will be private and making a complete backup copy is recommended before sharing the access details.

regards,
Waqar

#2695127

Thank you for sharing the access details, but I'm getting the incorrect password message.

Can you please check the username and password for the WP admin access again?
(I've set your next reply as private)

#2695371

Thank you for sharing the access details.

The use of the eval function, which is being used in the current 'calculate' shortcode, is generally not considered safe.

Here is a custom shortcode that you can use instead of to return the BMI value instead:


add_shortcode('custom_show_BMI', 'custom_show_BMI_func');
function custom_show_BMI_func($atts) {
	extract( shortcode_atts( array(
		'round' => 0,
	), $atts ) );

	$weight_value = types_render_field('weight', array("output" => "raw"));
	$height_value = types_render_field('height', array("output" => "raw"));

	if(!empty($weight_value) && !empty($height_value)) {
		$BMI = $weight_value/($height_value ** 2);
		return round($BMI, $atts['round']);
	}  
}

Usage Examples:


[custom_show_BMI]

[custom_show_BMI round='2']

[custom_show_BMI round='3']