Calculated fields adds a new setting field called “Formula” to the the Advanced Custom Fields editor for entering a mathematical expression.
When a post, page or user that has a custom formula field is saved to the database, the saved field value will be the result of evaluating the expression. The real power of the expression comes from the ability to use other fields as variables so that you can perform calculations based on other field values.
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.
Updating and Ajax
The main or core functionality of Calculated Fields for ACF is to evaluate the formula expressions at the time when a post is saved to the database. To make life as easy as possible for the administrator, Calculated Fields for ACF goes to great lengths to also update calculated fields in real time during the time of editing the post. This updating is managed using Ajax calls, so the actual calculations are always performed in PHP on the server side.
The Ajax based updating "just works" when editing posts custom fields in any of the normal meta boxes, regardless if these are displayed in the classic editor or in the block editor (Gutenberg).
The Ajax updating also "just works" in a few, but far from all, front end scenarios. We are actively supporting front end calculated fields when the front end forms are created using the built in "acf_form" from Advanced Custom Fields and with the ACF Elementor addon.
More reading
- The basics about expressions.
- All available operators
- All available functions
- Output formatting
- Some specifics about how to use rounding functions.
- Working with repeater and group fields