Synchronizing ACF Fields Across WP Layered Sites
I was looking for a way to sync ACF fields across sites in a multi-page WordPress site. There are 5 sites with separate content, but they all use the same ACF fields. I would rather not create manually and add these new fields on every site.
Is there a workaround?
Using WP 4.8 and ACF Pro 5.5.1.4
source to share
Since you are using ACF Pro, you can use the Export / Import function.
- Network Activate advanced custom fields
- Create field groups on the main site via the custom fields menu.
- Use custom fields -> Export, select all field groups, Export to PHP
- Inject PHP into your (child) themes functions.php
- Go back and remove the fields from the main site so that duplicates will repeat.
You now have ACF fields available across the web.
source to share
I thought about this on my own a couple of times and found that the easiest way is to create a github repository with a bunch of acf-field-name.php files, and then bring those repos as submodules to each of your projects. If you put these php files in acf folder in your theme folder and use a function in functions.php like this,
function getAcfFileNames()
{
return array(
'acf-one',
'acf-two',
'acf-three',
);
}
function add_php_acf_field_groups()
{
$fileNames = getAcfFileNames();
foreach ($fileNames as $fileName) {
include_once 'acf/' . $fileName . '.php';
}
}
;
add_action('acf/init', 'add_php_acf_field_groups');
This should work fine. And if you want to edit those acf.php files in the project, you can use -> https://github.com/BeAPI/ACF-PHP-Recovery to restore the php file locally and update it. After that, just export the file and upload it to the ACF repository.
Another thing that unfortunately I haven't found a better solution.
source to share
You can sync fields seamlessly as long as you attach them to template names and not to specific pages or posts.
https://www.advancedcustomfields.com/resources/synchronized-json/
Just make quick changes to your function.php file.
https://www.advancedcustomfields.com/resources/local-json/
I used the exported PHP code method when I finished working locally and want to update my working theme.
source to share
There is a wonderful Gist by Jesse Pearson that automatically syncs all acf-json files when an admin logs in:
https://gist.github.com/jessepearson/a537b2f78556cd705947
You can also find some code improvements in the comments on the gist, watch them!
Saved me a lot of time.
source to share