Calculated field enhancements in Advanced Forms Pro version 1.6.6

Version 1.6.6 of Advanced Forms Pro was released just a few days ago and with it came some interesting little enhancements to the calculated field that allow you to do the following:

  1. Trigger calculated field rerenders whenever needed.
  2. Intercept the rendered preview markup and append it anywhere you need it on the page.

This is all made possible by building on top of the Advanced Custom Fields JavaScript API which ensures the code you need to write is simple, clean, and stable.

Why would you use these enhancements?

These enhancements make it possible to build more complex form previews that update while the user enters their data in the form. For an example of this in action, just take a look at our plugin review form.

The ability to trigger updates whenever you like makes it possible to have the field rerender on keystrokes, button clicks, timer intervals, or any other type of on-page event that you can intercept via JavaScript.

The ability to control where the preview renders on the page offers even more possibilities as your preview will no longer be confined to the form’s markup – you can append the output to any HTML element on the page making it possible to have your form in one place and the rendered preview somewhere completely different!

How to trigger calculated field updates

Triggering calculated field update is really easy using any of the following formats:

// This is the simplest format available and will trigger the update for
// all calculated fields on the page
acf.doAction( 'af/field/calculated/update_value' );
// You may target specific fields by field name
acf.doAction( 'af/field/calculated/update_value/name=FIELD_NAME' );
// You may also target specific fields by field key
acf.doAction( 'af/field/calculated/update_value/key=FIELD_KEY' );

How to control where the rendered preview appears

Intercepting the return values is also really easy and offers the same field targeting capabilities as the triggers:

// This will intercept the rendered calculated field and insert the markup
// into an element with the id `#preview-container`
acf.addAction( 'af/field/calculated/value_updated', function( value, field, form ) {
$('#preview-container').html(value);
});
// You may intercept the return values of specific fields by field name
acf.addAction( 'af/field/calculated/value_updated/name=FIELD_NAME', function( value, field, form ) {
// …
});
// You may also intercept the return values of specific fields by field key
acf.addAction( 'af/field/calculated/value_updated/key=FIELD_KEY', function( value, field, form ) {
// …
});

Further reading

To learn more about calculated fields an Advanced Forms Pro check out the related docs:

About the author

Phil Kurth is a web developer living in Melbourne, Australia. Phil has a long history of WordPress development and enjoys building tools to empower others in their web design/development practice.

When not working with the web, Phil is usually spending time with his two young sons or is out hiking through the Australian bush.

Good dev stuff, delivered.

Product news, tips, and other cool things we make.

We never share your data. Read our Privacy Policy

© 2016 – 2024 Hookturn Digital. All rights reserved.