Honestly, the php and json are fairly similar. You could probably grab the field group code out of the php file and convert it to a json file easily with a few search & replaces.
My original point, essentially, was that you may be better served in the long run not using php to generate the fields. The code I included is a process for inserting the fields, from a json file, via php rather than manual import in Custom Fields > Tools.
Marko,
The solution I eventually came up with was to export my base set of fields as a json file. In my plugin I can then import that set of json fields, then edit them in the admin at will. Here is a chunk of my code that you may find useful:
<?php
function update_fields_from_json(){
// Find local JSON directory named 'fields'
$dir = new DirectoryIterator( dirname(__FILE__) . '/fields' );
// there should only be one json file in the fields folder (in my case)
foreach( $dir as $file ) {
if ( !$file->isDot() && 'json' == $file->getExtension() ) {
$json = json_decode( file_get_contents( $file->getPathname() ), true );
// if importing an auto-json, wrap field group in array
if( isset($json['key']) ) {
$json = array( $json );
}
foreach( $json as $field_group ) {
// group key is in the json file
$existing_fields = _acf_get_field_group_by_key("group_57796a3bdfb15");
// if the fields don't exist in the db import them now
if(!$existing_fields){
new jvp_acf_admin_notice('Adding All JVP ACF Fields');
acf_import_field_group($field_group);
}
}
}
}
}
?>
Hi John and Robert,
It is definitely possible, and the three of us are definitely not the ones to have tried / done such a thing. I’ve built a custom page builder for Bootstrap 3 for the exact same reasons, and will certainly update for BS4 when it’s out of alpha.
The challenge I’m currently tackling is how to update fields & layouts in a non-destructive way. One of my requirements for my page builder was that I needed to not only import json data, but to import in such a way that it exposed the fields in ACF (which is not default, I believe, when just importing the json) so that I could add additional layouts on a per-site basis.
However, adding custom layouts per-site makes it very difficult to update my base set of layouts. I’m working on this currently and have a routine down for detecting the correct layouts, deleting and updating fields. Where I’m stuck is on adding layouts, since they are not fields, but content in the flexible column’s post. Hopefully I’ll get over that hump quickly.
I’d be happy to compare notes when I’m finished with my current updates!
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.