White Label Coders  /  Blog  /  [Plugin] WLC ACF Post Save Helper

Category: Plugins

[Plugin] WLC ACF Post Save Helper

WLC ACF Post Save Helper WordPress plugin
30.10.2023
5 min read

A WordPress plugin that optimizes the process of saving posts with ACF fields.

License

GPL v2 or later.

Description

The plugin allows to speed up the process of saving the form in the WordPress backend consisting of ACF fields.

When filling out the form, the script detects the changed fields, marks them with
a red triangle (informative, for your convenience), and saves them to the database upon post save action.
All changed fields can be optionally saved in the database within one transaction. This guarantees the atomicity of the entire process (all fields are saved at once, or none in case of any processing error) and faster write speed (any command inserting or updating data in the database is carried out with a separate transaction – executing one transaction instead of several transactions speeds up the process).

The main benefit of the applied solution is to avoid saving all ACF fields, even those that have not been changed by the user. The performance gain increases along with the number of ACF fields in a form.

Installation

From WordPress plugins repository:

  1. Search for “WLC ACF Post Save Helper” on ‘Plugins’ -> ‘Add new’ screen.
  2. Click Install button.
  3. When installed, click Activate button or activate the plugin on ‘Plugins’ page.

Manually:

  1. Download the plugin’s zip archive.
  2. Extract all the files to /wp-content/plugins folder.
  3. Activate the plugin on ‘Plugins’ page.

Compatibility

The plugin uses ACF hooks (like ‘acf/save_post’ and ‘acf/prepare_field’). Advanced Custom Fields version 5.0 or higher is recommended.

The plugin is compatible with the ACF Database Tables addon – and allows to limit the number of queried database tables per single post save operation.

Screenshots

WLC ACF Post Save Helper settings
Main configuration page
WLC ACF Post Save Helper simple form
Form with changed basic fields
WLC ACF Post Save Helper complex form
Form with changed complex fields
WLC ACF Post Save Helper complex form 2
Form with changed complex fields
What post types are supported?

Some built-in post types are supported, along with all custom post types added via other plugins.
To enable the selected post type, go to WLC ACF Helper -> Settings and select desired options in the Enabled post types setting.

How can I change the plugin’s settings?

Navigate to WLC ACF Helper -> Settings

Why only fields marked with the red triangle are saved?

This is the main goal of the plugin – to save only changed fields and skip those that are left intact.

I created a custom ACF field and your plugin refuses to detect changes within it.

If your custom ACF field (or a field type registered by another plugin) cannot be detected as changed, then you can set it as ignored on WLC ACF Helper -> Settings page. There’s a setting called Ignored ACF field types (for all enabled post types) – you can select any field that shouldn’t be handled by our plugin and always updated in the database upon post save.

I don’t like these red triangles – how to get rid of them?

If you want to keep the plugin’s functionality and have the saving process optimized, but at the same time have the red triangles turned off, then go to WLC ACF Helper -> Settings and make sure Mark changed fields with a triangle setting is unchecked.

What is a transaction and should I disable it in settings?

By default, each SQL command sent to the MySQL / MariaDB server acts like a single transaction – the data is saved permanently to the underlying storage after each SQL command (autocommit mode).
Saving multiple ACF fields produces a separate transaction for each field and causes the autocommit to kick in multiple times. This hits the performance, but guarantees all data is saved, even if something goes wrong in the middle (only failed saves are rejected).
By using a transaction, the number of permanent saves is reduced to one. All SQL commands (like UPDATE or DELETE) that are executed within the post save action are part of a one transaction and have to succeed in order to make the changes permanent. Failure of any of the SQL commands (e.g. because of wrong attribute name, missing table, SQL syntax error etc.) rejects all changes made since the beginning of a transaction.
Sometimes such behavior is not desirable, that’s why you can disable it on WLC ACF Helper -> Settings page. However, keeping the Save all ACF fields in a transaction setting enabled is highly recommended.

What does this plugin actually do?

TL;DR: It speeds up the post saving process and ensures only changes fields are updated in the database.

Long version: The plugin detects changes made within selected post types’ forms and passes this information to ‘acf/save_post’ hooks, where data received from the form is processed. All values that come from unchanged ACF fields (not detected as changed in the UI) are ignored. As a result, only changed ACF fields are saved in the database. This improves the performance, especially in the case of very big forms (with lots of ACF fields).

Notes for developers

How it works?

The plugin hooks into ‘acf/save_post’ (with highest possible priority), removes unchanged ACF fields from $_POST[‘acf’] (only for enabled post types and not ignored ACF field types) and starts a new transaction. When ACF finishes its job, the plugin hooks again into ‘acf/save_post’ (this time with lowest possible priority) and commits the transaction.

All changes made during the post save operation are catched within the transaction and persisted in the database at once.

Note: Any other plugin that makes changes to the post during the save operation also falls into the transaction. There’s currently no way to distinguish SQL queries coming from different plugins. The only solution is to use a separate connection to the database server.

Default values for new posts

There are ACF fields, like True / False choice field, that have the initial value set right after a new post form is loaded. Such fields are immediately detected as changed, so their value can be saved in the database without user interaction.

A custom CSS class (–wlc-acfpsh–has-default-value) is added for such ACF fields and handled by the JavaScript logic on a new form load.

Settings

All settings are stored as WP options.

Setting nameWP option nameType
Enabled post typeswlc_acfpsh_enabled_post_typesarray of strings
Ignored ACF Field typeswlc_acfpsh_ignored_acf_field_typesarray of strings
Mark changed fields with a trianglewlc_acfpsh_show_changed_indicatorstring (‘yes’ or empty)
Save all ACF fields in a transactionwlc_acfpsh_use_transactionstring (‘yes’ or empty)

Hooks

  1. Filter ‘wlc_acfpsh_enabled_post_types’
    Arguments: ‘array $default’

    This filter allows to change the array of post types that are supported by the plugin. Any changes here overrides values stored in option ‘wlc_acfpsh_enabled_post_types’ during the request.
  2. Filter ‘wlc_acfpsh_ignored_acf_field_types’
    Arguments: ‘array $default’

    This filter allows to change the array of ignored ACF field types. Any changes here override values stored in option wlc_acfpsh_ignored_acf_field_types during the request.

Extending the plugin

There are a couple of things still to be implemented in future versions:

  • add an option to select the size, shape, and location of a change indicator (a red triangle by default)
  • add more WP filters and actions to allow the functionality to be extended in broader cases
  • add JS functions and events for 3rd party integrations

Have better ideas? Contribute, please!

Changelog

1.0.1

  • Updated documentation
  • Tested with the latest version of WordPress and ACF
  • Added WP hooks: ‘wlc_acfpsh_enabled_post_types’ and ‘wlc_acfpsh_ignored_acf_field_types’

1.0.0

First release.

Maciej Kubiak

Data Engineer / PHP & JavaScript Programmer / WordPress Expert

Experienced in designing and building dedicated systems, creating WordPress plugins and integrating web services. Optimizations enthusiast who always finds a way to improve existing processes, especially database-related ones. Currently involved in an international team as a tech leader and data migration specialist.

Related Articles
SEE OUR BLOG
Check related articles
WordPress Development Workflow
The WordPress development workflow – the best practices

The purpose of this article is to describe how we work on Wordpress Development at White Label Coders, and provide an inspiration for everyone who currently is seeking for good practices to follow and setup his own way of working with WordPress site.

Read more
Adobe XD vs Invision vs Figma website design
Figma, Sketch, Adobe XD or InVision? Web design tools comparison.

The process of web design depends on many professionals and the cooperation between them. Their workflow must be carefully planned and discussed beforehand for the process to go smoothly.

Read more
my wordpress webiste got hacked
My WordPress website has been hacked. What should I do?

Any WordPress website could be hacked, and when it happens, you may be tempted to panic. Don't. Calm down and just read how to ...

Read more
White Label Coders Best B2B Companies in Poland by Clutch
Clutch names White Label Coders as one of the Best B2B Companies in Poland

There are many companies that are investing a lot of time, effort, and money in creating the best websites for their brand.

Read more
Web Development
What is web development like?

For the majority of people, including some of our new clients, the processes of web development and web design mean pretty much the same thing - they basically come down to drawing a website or application that looks fantastic and then uploading it to the Internet.

Read more
delighted programmer with glasses using computer
Let’s talk about your WordPress project!

Do you have an exciting strategic project coming up that you would like to talk about?

wp
woo
php
node
nest
js
angular-2