Calculated fields for ACF

(4 customer reviews)

$0.00

Adds a formula fields ot Advanced Custom Fields that allows you to perform simple math based on field values.

Calculated Fields for ACF is also available from the official WordPress plugin repository

Category:

Current version: 1.3.0
Requires WordPress Version: 4.7
Requires PHP Version: 7.0

Description

Adds a formula fields ot Advanced Custom Fields that allows you to perform simple math based on field values. From version 1.1 calculations are done while editing fields in the admin screen. When the post is saved, all fields are recalculated before being saved.

Requirements

Calculated fields requires Advanced Custom Fields from the WordPress repository version 5.0 or later and works with the Pro version as well as the standard version.

Formulas

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 (new in version 1.1).

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):

  • fieldA * 2
  • fieldA * fieldB
  • abs(fieldA) * (2 + sqrt(fieldB))
  • amount * parent.price

Conditional operators (new in version 1.2.0)</h4)

An expression can also contain a conditional expression that will return either 0 or 1. The supported operators are:

  • Equals (==) – Returns 1 when two values are equal, 0 otherwise
  • Not equals (!=) – Returns 1 when two values are not equal, 0 otherwise
  • Greater than (>) – Returns 1 when the first operand is greater than the second, 0 otherwise
  • Less than (<) – Returns 1 when the first operand is less than the second, 0 otherwise

Examples of valid expressions using conditional operators:

  • 10 == 10 (returns 1)
  • 10 == 2 (returns 0)
  • 10 > 2 (returns 1)
  • 10 < 2 (returns 0)

Rounding functions (new in version 1.2.0)

Round a decimal value to the nearest integer. Supported functions are:

  • round() – Uses standard mathematical rounding rules to round to nearest integer
  • ceil() – Rounds the value to the next higher integer
  • floor() – Rounds the value to the next lower integer

Examples of valid expressions using rounding functions:

  • round(10.2) (returns 10)
  • round(10.9) (returns 11)
  • round(10.888888 * 10) / 10 (returns 10.9)
  • ceil(10.2) (returns 11)
  • floor(10.2) (returns 10)

Array functions for repeater fields

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:

  • sum()
  • average()
  • count()
  • min()
  • max()

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, parantheses can not be used. If you need to aggregate a more complex calculation, you should add an extra field in the repeater group (hidden if needed) and let the aggregate function work on this extra field.

Data types and calculation order

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 for ACF is a free plugin from wundermatics.com

Calculated fields for ACF

What’s the required version of Advanced Custom Fields

Calculated fields for ACF requires Advanced Custom Fields 5.0 or later

Does this work with the Pro version

Yes. Calculated fields for ACF works with both the free and the pro version.

Minimum requirements

  • PHP 7.3 or newer

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.

Install from within WordPress

  • Visit the plugins page within your dashboard and select ‘Add New’
  • Search for ‘Calculated Fields for ACF’
  • Click ‘Install’ and wait until the button caption changes to ‘Activate’
  • Click ‘Activate’

Install manually

  • Download the zip file
  • Upload the calculated-fields-for-acf folder from the zip to the /wp-content/plugins/ directory on your server
  • Navigate to the Plugins page in WordPress admin and locate the plugin
    Click ‘Activate’

1.3.0

  • Feature: Support for formatting numeric output using PHP sprintf syntax
  • Feature: Optionally let formula return empty string instead of zero
  • Fix “empty needle” bug that emits PHP warning messages to the error log in certain field groups

1.2.4

  • Fixes problem with calculations of fields inside a group

1.2.3

  • Fixes problem with calculations not working in front end forms created with acf_form()

1.2.2

  • Fixes issue with operator == always returning false

1.2.1

  • Adds support for conditional operators >=, <=

1.2.0

  • Adds support for conditional operators ==, !=, > and <
  • Adds support for rounding functions floor, ceil and round

1.1.3

  • Fixes issue with ajax recalculation not triggered by removing a row from a repeater field
  • Fixes issue with ajax recalculation not triggered by changing a dropdown/select field
  • Fixes multiple issues related to ACF Blocks. Should work as expected now, but we’re considering support for ACF blocks “beta” for now

1.1.1

  • Fixes typo in javascript file that causes more ajax recalculations that needed
  • Fixes issue with some PHP versions and empty object keys (https://bugs.php.net/bug.php?id=46600)
  • Fixes issue with ACF enumerating rows using alphanumeric keys in some cases (older versions)

1.1.0

  • Adds in-edit recalculations. Calculated fields are updated while still in edit mode
  • Adds read only option. All fields can be set as read only in the ACF field editor (not just calculated fields)
  • Adds parent. prefix for formulas in sub fields in a repeater field.

1.0.1

  • Fixes issue with complex custom fields types that can’t be converted to string/numeric
Let us know if you're looking for support for a specific product or add-on.

4 reviews for Calculated fields for ACF

  1. nick6352683

    What took this so long???

    A plugin like this is way overdue !!! Works as documented, and I have 2 quick feature requests…

    1. The calculated values are calculated AFTER the post is saved, and displayed AFTER the post gets refreshed. (I’m guessing) using Ajax to calculate the fields on the fly and display the results without a post save/refresh will make this 1000 times better…

    2. The calculated fields should be read only… right now, even though you can enter any value in a calculated field, your manually entered value will be overwritten when you save the post and the actual correct calculated value will overwrite whatever you had manually entered in that calculated field.

    Nevertheless, this is probably one of my favorite if not the most favorite ACF plugin !!!

    Nick.

  2. Samuel Bauer

    A game changer

    … for my individual product compositions with ACF Pro. The repeater calculations are epic. THANK YOU

  3. larssteffensen

    Sooooooo useful!

    I need to have options for psychological testing for users.
    Is very useful for people, but can not be done without adding up user input!
    This is a godsend! Thank you!

  4. Demode

    Great addition for ACF – Thank you guys! Drinks are on me next time you’re in Croatia ?

Add a review

Your email address will not be published. Required fields are marked *