3.1.2.1. Note on checkbox groups
3.1.2.2. Running PHP code inside the formula (Pro version only)
7.1. Triggering calculation programmatically
7.2. Post-calculation event handling
Contact Form 7 version 5.0 or later is required.
1. Download the pvb-contact-form-7-calculator.zip file to your computer.
2. Unzip the file.
3. Upload the pvb-contact-form-7-calculator directory to your /wp-content/plugins/ directory.
The plugin adds support for the following special tags that you can add to any CF7 form:
A calculated value is a read-only input element where a value is calculated according to a predefined formula.
You can have only one calculated value per form.
[calculation total min:0 max:1000 precision:2 "item_price * quantity"]
The calculation formula is a mathematical expression that may contain the following elements:
· Number literals;
· Values taken from other fields – simply use the field name in the equation. Non-numerical values are evaluated as 0;
· Parentheses – for grouping operations, e.g. (a+b)*c
· Operators:
o addition (a + b)
o subtraction (a - b)
o multiplication (a * b)
o division (a / b)
o exponentiation (a ^ b)
o modulo (a % b)
· Functions:
o Square root: sqrt(x)
o Logarithm: log(number, base)
o Extract day number from date field: fn_day(x)
o Extract month number from date field: fn_month(x)
o Extract year from date field: fn_year(x)
o Extract day of the year from date field: fn_day_of_year(x)
o Extract weekday number from date field: fn_weekday(x) – Monday is 1, Sunday is 7
o Extract number of business days between two date fields: fn_business_days(x, y)
With multiple checkbox groups, it is possible to select more than one value. In that case, if you use a checkbox field name in your formula, all the selections will be automatically summed. For example this form code:
[checkbox mycheckbox "1" "2" "3"]
[calculation result "mycheckbox + 1"]
could yield a result like this:

To run PHP code inside the formula, enclose it within a fn_php( … ) call.
· Running PHP code is disabled by default since it is a significant security risk. You can enable it from the plugin options page, but only if you are 100% sure you know what you are doing!
· Do not add any PHP opening/closing tags (<?php … ?>).
· Use a return statement to set the value that will be used in the calculation formula.
· You can escape double-quotes in your PHP code using the " entity.
· You can use the $_POST global to access form data.
· Since Contact Form 7 uses square brackets - “[“ and “]” - to delimit form tags, square brackets in your PHP code will not be handled properly. If you need to access array elements, please use curly braces. In PHP, both square brackets and curly braces can be used interchangeably for accessing array elements (e.g. $array[42] and $array{42} will both do the same thing).
· Minimum – by specifying the “min:X” attribute, you can set a minimum value for the result of the calculation. For example, if you specify “min:0”, when the result is negative, it will always be set to zero instead.
· Maximum – by specifying the “max:Y” attribute, you can set a maximum value for the result of the calculation. For example, if you specify “max:100”, when the result exceeds 100, it will always be set to 100 instead.
· Precision – optionally, you can specify the “precision” attribute, which specifies the maximum number of digits after the decimal point. For example, with “precision:2”, 0.786 will be rounded to 0.79. You can specify “precision:0” to get only integer results.
· Roundup – if the “roundup” attribute is present, when rounding to match the “precision” option, the calculator will always round up to the nearest larger integer.
· Rounddown – if the “rounddown” attribute is present, when rounding to match the “precision” option, the calculator will always round down to the nearest smaller integer.
· Initial value – if the “init” attribute is present (e.g. “init:100”, the field will be pre-filled with the given number before any calculations are performed.
The calculate button will trigger an AJAX request that will fill in all the calculated values in the form.
[calculate_button "Click here to calculate"]
[calculate_button my_calculate_button autocalc cf7-hide "No click needed – this is a hidden, automatic Calculate button"]
You can add the “cf7-hide” option to a form field to make it hidden. For example:
[calculation total cf7-hide "item_price * quantity"]
[variable price cf7-hide "text:" "One 1" "Two 2" "Three 3" "Four 4"]
Each of the three calculation elements
supported by this plugin have their own class names, respectively:
wpcf7-calculation, wpcf7-variable, and wpcf7-calculate_button.
You can apply CSS styles to those classes, for example:
.wpcf7-calculation { border: red }
You can trigger calculation programmatically from your own Javascript code by calling the cf7Calculate function. It takes a single argument a jQuery object constructed from the respective <form> element. For example:
<script>
jQuery(document).ready(function($){
var recalc = function() {
// Find any forms on the current page and activate the calculations
cf7Calculate(jQuery('form'));
};
jQuery('#mybutton').on('click', recalc);
});
</script>
After the calculation AJAX call is made, the plugin will trigger the wpcf7calculate jQuery event. If the AJAX call fails, wpcf7calculatefail will be triggered. You can code your own handlers for these events, for example:
<script>
jQuery('form#calculator').on('wpcf7calculate', function() {alert('Your calculation is ready!');
}
</script>