Is your WordPress website using a lot of ACF and is your backend slow? Do you want it to work faster, and save queries in your database? Pay attention, because I will tell you some keys to keep in mind because your site may run as if you did not even have ACF installed.
Custom fields make the WordPress database fat and cumbersome. Not only the _postmeta table, but also the _options table.
It doesn't just happen with ACF, this is normal, any plugin that adds custom fields to our entries (Woocommerce, etc.) will make the database denser and therefore internal queries to it will be slower and slower.
This can make the backend slow, and it can be tedious to edit and save entries, or simply move around within our administrator. Especially if you are using cheap shared hosting, where the memory resources and limits of our PHP configuration will be more limited.
The solution should not always be to acquire more powerful hosting; our site may be medium-sized and the cost may not compensate us. Well, it's time to optimize, which is possible.
Optimize the backend and database by generating files in your theme.
It may look complex, but it is not that complex. In fact, the database may not need to be touched. You just have to separate certain information that does not necessarily have to be within it.
Pass to our theme (files) everything that should NOT be in our database.
Anything that can be in files, put it in files. Access to these is direct and they are searched more easily. Anything, not just ACF, any style, script, function, if it can be in your functions.php, or in included files, will be accessed faster than if it is in your database.
Plugins make our lives easier, but we have to know when certain things should be added to the theme. Some examples of information that we can have in our database and that can be externalized to files is:
- Styles in the custom CSS of the backend, or of some plugin fields
- Definitions of Custom Post Types or taxonomies created with a visual generator, such as CPT UI or similar.
- Custom Field Group Definitions
- Definitions and info in field boxes of builders, visual composers and visual editors, such as Elementor, Divi and similar (this is NEFASTO).
The CSS is as simple as removing it from the backend and including it in the CSS of the theme, either in the style.css of our child theme, or incorporating it into the styles loaded from our theme using the enqueue function. Even bag a <link rel=»stylesheet»… en el It is better than having it in the admin. If you then minimize or join together, even better. Everything must always be searched.
You must also pass the custom post types and/or taxonomies to the php of your template. It is not bad to use a generator that allows you to design and manage your CPTs, but once you have them, use the tool that these plugins offer you to generate the PHP code that registers these types of content and pass it to the theme. Being read from the theme we will save queries to the database every time the backend is loaded.
We are going to focus now on the third point, of the fourth I can only tell you that never use Elementor or similar. Switch to the WordPress block editor, Gutenberg, because it was created to improve and sweep away the chaos and destruction that these visual editors generate on WordPress sites.
Speed up ACFs by generating PHP or JSON files.
Our groups and fields, created using ACF can be passed to PHP or JSON files. The fastest and most efficient method is to generate the PHP code that registers our fields and incorporate it into our theme. But once they are in this format, they are more "unmanageable" when it comes to making changes or being reused in different projects. For this reason, there is the JSON method, which is more versatile.
The generation of JSON files of our custom fields group definitions is automated if we create a “/acf-json” directory in the root of our template or theme.
If there is no user/permissions issue on the file system, every time we save changes to a group of fields, the group_xxxxxxx.json file should be generated in this folder. If it doesn't work for us, I will explain the steps to do it manually. It is very simple:
- Using the export tool, export your group to a JSON file. Do not export more than one group at a time, you must do it one at a time. Edit the file.
- First, copy the group name from the beginning of the json, and save the file using that name dot json. Or rename acf_export…json to group_xxxx.json.
- Now you just have to delete the beginning and end [ and ] symbols from the exported json array.
- Finally, after the last data of the json array, "show_in_rest": (0 or 1), we will add another line with the data 'modified':TIMESTAMP where timestamp we will put a timestamp of the current time. Use google and search “current timestamp”, you will get the current date and time in timestamp format. Copy the number and paste it into the modified value.
- Upload the group_loquesea.json file using git or ftp to the acf-json folder of your theme. And that's it, your wp will give priority to your json.
- If you have many groups of fields, and you pass them all to files, either using json or php, you will speed up your site by 1000%, guaranteed.
ACF even faster than shit: ACF Custom Database Tables
The panacea is already something super pro that I am going to tell you now. The information of the custom fields (not their definitions, but their values) continues to fatten our termmeta or postmeta. Reading these meta fields from each post is also tedious for the performance of our database.
If you have ever optimized a database, you know that, for example, after each WooCommerce update, your admin notifies you that work is being done on it in the background. It is a cleaning operation and also an index generation operation. We can generate indexes and optimizations of our tables using phpMyAdmin, or with plugins like WP Optimize, highly recommended tools. But I'm going to tell you 2 more plugins that will make your site take 5 red bulls:
Exclusive tables for your custom fields with ACF Custom Database Tables LIKE A BOSS
This plugin will generate separate tables from the acf that we tell it. The data for each post will be saved in the new table but will also continue to be saved in postmeta. But, the value of each field will be looked at first in the custom table. So the speed will increase, because that board will be infinitely smaller than the post-goal board.
Bonus track (the latter for your entire database, not just because you use ACF): Add high-performance indexes to your database tables with this plugin:
If you follow the steps in this mini-guide, your WordPress will fly and your backend will run smoothly. And you, do you also have any good tips for optimizing acf so that the admin doesn't slow down? Well, share it in the comments! That's what they are for!
Appendix: Performance comparison of custom fields in DB, json and php
Recommended reading Twitter thread:
An interesting question some people keep asking me. I wanted to write an article to explain it. But let's make a thread instead!📜 https://t.co/aXCb4nHPEz
— hwk (@hwkfr) March 22, 2020
Leave a Reply