Category: Plugins
[Plugin] WLC ACF Post Save Helper
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:
- Search for “WLC ACF Post Save Helper” on ‘Plugins’ -> ‘Add new’ screen.
- Click Install button.
- When installed, click Activate button or activate the plugin on ‘Plugins’ page.
Manually:
- Download the plugin’s zip archive.
- Extract all the files to /wp-content/plugins folder.
- 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
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.
Navigate to WLC ACF Helper -> Settings
This is the main goal of the plugin – to save only changed fields and skip those that are left intact.
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.
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.
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.
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 name | WP option name | Type |
---|---|---|
Enabled post types | wlc_acfpsh_enabled_post_types | array of strings |
Ignored ACF Field types | wlc_acfpsh_ignored_acf_field_types | array of strings |
Mark changed fields with a triangle | wlc_acfpsh_show_changed_indicator | string (‘yes’ or empty) |
Save all ACF fields in a transaction | wlc_acfpsh_use_transaction | string (‘yes’ or empty) |
Hooks
- 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. - 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.