Skip Navigation

[Resolved] Adding field values

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to add/sum the values of custom fields?

Solution:
1. Add this code in your theme’s or child theme’s functions.php file:

add_shortcode('wpv-calculate', 'calculate_shortcode');
function calculate_shortcode($atts, $content = null) {
     
 $content = wpv_do_shortcode($content);
    $content = @eval("return $content;");
    return round($content);
}

2. Then use the below shortcode:

[wpv-calculate][types field='judge-1-score'][/types] + [types field='judge-2-score'][/types][/wpv-calculate]

Change the field names / slugs as needed.

This support ticket is created 7 years, 2 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
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 8 replies, has 2 voices.

Last updated by Noman 7 years, 2 months ago.

Assisted by: Noman.

Author
Posts
#555045

I'm try to add the values of fields in a similar way to this : https://toolset.com/forums/topic/calculator/

adding the following to my functions file:

add_shortcode('wpv-calculate', 'calculate_shortcode');
function calculate_shortcode($atts,$content=null) {
$content = wpv_do_shortcode($content);
$content = eval("return $content;");
return $number;
}

then added this in views:

<td>[wpv-calculate][types field="judge-1-score"]+[types field="judge-2-score"][/wpv-calculate]</td>

But I'm getting the following error:

Parse error: syntax error, unexpected ';' in /var/sites/a/awards.isleofwightwebhosting.co.uk/public_html/wp-content/themes/iwchamber/functions.php(13) : eval()'d code on line 1

thanks

Paul

#555128

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Paul,

Thank you for contacting Toolset support. Please use the following updated code, you may change it as needed:

add_shortcode('wpv-calculate', 'calculate_shortcode');
function calculate_shortcode($atts, $content = null) {
	
	$content = wpv_do_shortcode($content);
	$content = eval("return $content;");
	return round($content);
}

Shortcode will be like:

[wpv-calculate][types field='judge-1-score'][/types] + [types field='judge-2-score'][/types][/wpv-calculate]

Related ticket: https://toolset.com/forums/topic/math-with-custom-fields-values/

Thank you

#555273
Screen Shot 2017-08-01 at 16.35.07.png

Hi Noman,

thank for your help but I'm still getting an error in the wpadmin See attached screenshot. all the fields being added have a values.

thanks

Paul

#555282

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello,

The code is working fine at my end. To further debug this issue I need to take a look at your setup and to request temporary website WP-admin and FTP info for your site to debug further and to help in resolving this. Your next answer will be private which means only you and I have access to it.

=== Please backup your database and website ===

✙ I would additionally need your permission to de-activate and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important.

✙ Please add the Link to the [View] Edit Screen. Thank you

#555294

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for providing login info, Can you please close out the "Results" page, I am going to work on it and check few things. Right now looks like you are already editing the page and hence I can't access it. Thanks

#555295

Hi Noman,

all logged out 🙂

Thanks

Paul

#555325

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

I have updated code and now error is resolved:
hidden link

hidden link

add_shortcode('wpv-calculate', 'calculate_shortcode');
function calculate_shortcode($atts, $content = null) {
    
 $content = wpv_do_shortcode($content);
    $content = @eval("return $content;");
    return round($content);
}

Thank you

#555363

Thanks so much, can I ask what the difference between $content = eval("return $content;"); and $content = @eval("return $content;"); ?

thanks

Paul

#555775

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Sometimes eval function throws strange unwanted notices, in that case we use @eval. However the eval function didn't show any warning in my test site with Twenty Sixteen theme.

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