Last active
April 26, 2024 08:22
-
-
Save JacobDB/a18b316eeb822aeaf7b25b3cc75d982c to your computer and use it in GitHub Desktop.
Load exportd JSON ACF fields via PHP – https://dev.to/jacobdb/import-acf-fields-via-php-without-losing-importability-5a8o/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Read ACF fields from JSON | |
*/ | |
function PREFIX_acf_register_json_fields() { | |
if (function_exists("acf_add_local_field_group")) { | |
$acf_json_data = locate_template("path/to/advanced-custom-fields.json"); | |
$custom_fields = $acf_json_data ? json_decode(file_get_contents($acf_json_data), true) : array(); | |
foreach ($custom_fields as $custom_field) { | |
acf_add_local_field_group($custom_field); | |
} | |
} | |
} | |
add_action("init", "PREFIX_acf_register_json_fields"); |
thanks for the quick reply!
I figured out my issue. I was trying to put the function to load the json in a plugin rather than my functions.php file.
moving things made it work as you described. Thanks for helping me work through that!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@joshespi I just tested this, and it seems to be working correctly for me with the latest versions of WordPress and ACF Pro. The below code will add a text field called "Test" to all posts in the
post
post type.Here's my PHP, in functions.php:
And here's my export file, in the root of the theme, named acf-export-2023-04-12.json: