Simple field math for Advanced Custom Fields.
Adds a formula fields ot Advanced Custom Fields that allows you to perform simple math based on field values. Calculations are updated dynamically via ajax while editing a post on the backend post editor as well as on front end forms.
Calculated fields requires Advanced Custom Fields 5.0 or later and works with Pro as well as the standard version.
Beginning in version 1.2.3, Calculated fields for ACF supports frontend forms created using the acf_form() function (built in to Advanced Custom Fields).
Calculated fields adds a new setting “Formula” to the the ACF field editor. Formulas supports referring to other fields in the same Field group using the field name. If a formula is added to a sub field inside a repeater field, the name refers to another sub field in the same repeater.
A sub field inside a repeater field can also refer to a parent field using the “parent.”” prefix.
A formula can contain the basic mathematical operations: plus (+), minus (-), multiply (*), division (/) and power of (^). Formulas can also contains functions like sin, cos, arcsin, log, ln, sqrt, abs. Parentheses are supported.
If an expression can’t be evaluated due to invalid syntax or referring to undefined field names, it will silently return zero.
Examples of valid expressions (note, no equal sign at the beginning):
An expression can also contain a conditional expression that will return either 0 or 1. The supported operators are:
Examples of valid expressions using conditional operators:
Round a decimal value to the nearest integer. Supported functions are:
Examples of valid expressions using rounding functions:
Note that the round() function only takes one parameter and always round to an integer. If you need to round to a higher precision, multiply and divide as shown in third example above.
Fields defined as part of a group, subfields, can be addressed using the group name as a prefix. Inside the group, a field defined on the parent level can be addressed using the “parent” prefix. See examples below:
Valid formula to use the value of field “foobar” defined inside the group “group2”:
* group2.foobar + 10
Valid formula in a field inside a group (assuming the parent has a field named count):
* parent.count * 22
If repeater fields are used, a calculated field in the parent field group can summarize a specific repeater sub fields using the aggregation functions sum, count, average, min or max. For example, the expression: “Sum(orderlines.amount)”” will return the sum of all the “amount” fields in the repeater field “orderlines”.
The available array functions are:
Note that anything after the dot (.) in the aggregate expression is an expression in itself. It’s perfectly OK to write a formula like: “Sum(orderlines.price * amount)”. This expression will walk over all lines in the “orderlines” repeater field, perform the calculation “price * amount” and return the sum of all lines.
Note that when working with aggregate functions, parentheses can not be used. If you need to aggregate a more complex calculation, you should add an extra field in the repeater group and let the aggregate function work on this extra field.
Calculated fields works with the assumption that all fields are defined as numeric in the ACF editor. Using a text field in a formula WILL PRODUCE UNPREDICTABLE RESULTS.
Calculations are made as the custom field is stored to the database. To minimize the impact of performance, Calculated fields rely on field order. A formula field can only refer to fields ordered BEFORE itself. A formula field that refers to a field ordered after it WILL PRODUCE UNPREDICTABLE RESULTS.
Calculated fields will for ACF run on PHP 5.6 or newer, but we highly recommend following the official WordPress guidelines that currently recommends PHP 7.3.