Home › Forums › ACF PRO › Getting data from JSON – ACF Plugin › Reply To: Getting data from JSON – ACF Plugin
A really simple example isn’t really possible because there are a lot of moving parts, I might be able to give a better outline.
// create an acf/save_post action
add_action('acf/acf_save_post', 'import_json_for_field_group_XX', 2);
function import_json_for_field_group_XX($post_id) {
// first to a check of the post type
if (get_post_type($post_id)) != 'your-post-type') {
// not our post type
return;
}
// check the radio field to see if it is set to the manual value
if (get_field('your-radio-field-name', $post_id) == 'manual') {
// not doing import
return;
}
// you may need to set up a field name => key reference array
// when importing you need to use the field keys
// either that of the file you are importing from
// must use the field keys in the JSON
$fields = array(
'field-name-1' => 'field_123456',
'field-name-1' => 'field_234567',
'field-name-1' => 'field_345678',
'field-name-1' => 'field_456789'
);
// doing an import
// get the JSON field
$json = get_field('json-input-field', $post_id);
// at this point I'm not sure what to do
// it depends on where the json file is stored
// you need to use PHP and maybe some WP functions
// find, open and get the contents of the file
// and store the content is a variable, I'll use $contents
$values = json_decode($contents, true);
// loop thought the fields in $values and update them
if ($values) {
foreach ($values as $field => $value) {
// update the field
// note that I am using the field key reference array I created above
// if you are importing with field keys this would not be needed
if (isset($fields[$field])) {
update_field($fields[$field], $value, $post_id);
}
} // end foreach $value
} // end if $values
// finally alter the radio field to manual
update_field(('your-radio-field-name', 'manual', $post_id);
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.