It doesn’t belong in the database — WordPress + ACF
Recently I’ve been working for a client in pair programming (with another developer) and quickly realize there are still questions about how to approach custom fields inside WordPress.
First of all, let’s recap what is ACF — Advanced Custom Fields for WordPress Developers. Advanced Custom Fields is a WordPress plugin which allows you to add extra content fields to your WordPress edit screens. These extra content fields are more commonly referred to as Custom Fields and can allow you to build websites faster and educate your clients quicker.
ACF Inside WP Admin
ACF come with two ways to work with. The first and easy one, it’s to use it out of the box and create group fields inside WordPress admin, bind it to a page (or options, or anything else) then use it inside your code.
It works really well, but the problem with that is if you lose your database, you’ll not only gonna need to re-create all your content, but also all your website structure.
It probably won’t happen… but there’s more!
The real struggle here is when you work with a team member, if you add fields to affect the database, your pair would have to re-create your fields and configurations to be sure they have what they need.
It would then require a method to sync or use a remote database and it can slow the creating process.
Register Fields Via PHP
The other way is to declare fields programmatically. It means you’ll have to create fields, which gonna be imported into your theme, that will create the fields and attach them to the right part of your theme (page, options, etc.).
To demo you the way to work, let’s add a subtitle field to all pages.
First, let’s create a new file, called
/themes/demo/includes/custom-fields and inside your
functions.php just add a loop through inside the includes folder to register all files.
And just like that, if you go inside one of your pages, the subtitle will be appearing!
The cons of this method is that you’d have check documentation more often before you get use to it. But in the end, it will speed up your entire dev process and get you closer to a collaborative approach.