Support

Account

Home Forums Add-ons Flexible Content Field Use PHP to add a field which can be managed in Admin?

Solved

Use PHP to add a field which can be managed in Admin?

  • I have a feeling what I want to do here won’t be possible, but I hope it is!

    I’d like to use PHP, or JSON to create a flexible content field, in this case called “Layout Blocks”. I then want this flexible content field to have a few layouts also added by code.

    I then want this flexible field to appear in the WP admin under Custom Fields, so that it can have new layouts added to it.

    The idea behind this is that I can include a default set of ACF layouts in my theme without restricting the user who installs the theme to my choice of layouts.

    Any ideas or solutions welcome!

  • I’m pretty sure that what you want to do can be done using ACF5 and Local JSON, but cannot be done in ACF4

    For more information:

    http://www.advancedcustomfields.com/resources/local-json/

    http://www.advancedcustomfields.com/resources/synchronized-json/

  • Ok could be moving in the right direction…

    So I didn’t mention that this is using a parent / child theme relationship. I want the JSON to live in the parent theme and then for the child theme to read that JSON and add it to the admin.

    I’ve tried adding a folder in my parent theme “acf-json-defaults”, and this code to my functions.php.

    function my_acf_json_load_point( $paths ) {
        
        // remove original path (optional)
        unset($paths[0]);
        
        // append path
        $paths[] = get_template_directory_uri() . '/acf-json-defaults';
        
        // return
        return $paths;
        
    }
    add_filter('acf/settings/load_json', 'my_acf_json_load_point');

    But it only seems to work if I put the “acf-json-defaults” folder in the child theme. Even then, when I save the field in admin, it creates a new JSON file with the same key in the “acf-json” folder.

    Does that make sense??

  • I don’t think that you’re using the right function. ACF is looking for a path and not a URI, so that might be affecting it. Try get_template_directory()

    
    function my_acf_json_load_point( $paths ) {
        
        // remove original path (optional)
        unset($paths[0]);
        
        // append path
        $paths[] = get_template_directory() . '/acf-json-defaults';
        
        // return
        return $paths;
        
    }
    add_filter('acf/settings/load_json', 'my_acf_json_load_point');
    

    Saving works independently of loading. If you have an acf-json folder in the current them folder ACF will always save the json there. There can only be one save point and that can be changed as well with the acf/settings/save_json hook.

  • Also reading the documentation:

    “JSON field groups are determined ‘available for sync’ when either the JSON field group does not exist in the DB (field group key is used to match pair), or when the JSON field group contains a higher ‘modified’ value than the DB post’s modified date.”

    That seems to be the wrong way round for what I want to do. I want the Database to be a higher modified value than the JSON. Unless I am missing something?

  • Nevermind, this seems to work 🙂

    I added a default layout in my acf-json-defaults file, then was able to add a new field via the admin.

    I did then try to add another field to the JSON, to see if that would then sync with the admin to add the new field, but that didn’t work. Any ideas or is that asking too much?

  • I think that once you modify the field group through the admin that will pretty much end the ability to sync the file or update it through the original json file.

    After that, for example, if you update the parent theme to include more fields then it will not automatically add those fields to the child theme setup.

    I was actually just looking at the import code in ACF, because it seems like that’s actually what you want to do, automatically import the field group. But importing is not really something that can be done through code, at least not by calling a single function. I was also thinking about updating the field group if if modifications were made to the theme and field group needed to be modified to reflect theme changes. Heh, that’s where my thinking spiraled out of control thinking about what would be needed to actually build the field group so that ACF would recognize it and then manually making changes to the posts used by ACf to make changes…

    Not sure if any of this rambling helps your project at all, but if you’re thinking about doing any of what I thought about, I think it would be a messy process, but not something that is altogether impossible for someone willing to dig around in the ACF code to figure out how.

  • Thanks, I appreciate you taking the time to think about it! As much as I’d love to, right now I simply don’t have time to do the digging. I’ll put this idea on hold for now but if anyone decides they want to look into it, let me know!

  • Heh, that’s what I do, think and dig when I think I might have a use for something. Unfortunately I don’t have much free time either. But I’ll be keeping this in the back of my mind for a time when I could be useful. I build plugins using ACF and this could be a way to allow one to be extended. Usually the first opportunity I get to put the research time against a paying client is when I really dig into stuff like this. 🙂

  • I’d certainly consider paying for something like this, if you’d be interested in giving it a go, we should talk more!

Viewing 10 posts - 1 through 10 (of 10 total)

The topic ‘Use PHP to add a field which can be managed in Admin?’ is closed to new replies.