Support

Account

Home Forums Feature Requests Convert PHP register_field_group array to JSON

Solving

Convert PHP register_field_group array to JSON

  • ACF 5’s JSON feature is very handy, and it should make development with multiple people very easy.

    I have a great number of sites which previously had their custom fields saved using the register_field_group function. It would be much nicer if these were in JSON.

  • Wanting the same feature!

  • Why don’t you use json_encode() to create a JSON version of the array that you pass into register_field_group(), then all you need to do is create group key for the fields (I think it may have been the id field in previous versions), then create a json file following the acf conventions and drop it into your theme’s acf-json folder…

    I have just done this and was able to sync the fields back into the database.

  • Yes please, could be really handy ! Thx

  • Are you looking for some way to export fields from a site with ACF 4 so that you can use them in ACF 5? Or just for use in ACF 4 for creating fields in code?

    For either one of these, while I’m not the developer, I can say that this will probably not happen. ACF 4 is no longer in active development and the only work done on it are security fixes and to make sure it keeps working with the latest version of WP. The developer has plans to eventually replace the version in the WP repo with a version of ACF 5, but I don’t know where that’s at.

    In either of the cases I would suggest looking at @morgan’s suggestion.

  • @hube2: I often used to define the fields for a site in PHP, with the old register_field_group function. There are many sites that I built like that which I have updated to ACF5.

    As I’m sure you know, the JSON save/sync features of ACF5 are much better. Ideally, ACF would read that file and output a bunch of JSON files in the appropriate place.

    In the meantime, I did a quick and dirty little php file, similar to @morgan’s suggestion. If anyone wants to use it, just paste the contents of a field group into the $array variable, then set the $themePath to the absolute path to your theme.

    You have to do this separately for each field group, so it can be slightly laborious if you have many of them. I tried (for 5 minutes) to parse the entire file, but I couldn’t figure out the regex for extracting each array, so I did it the quick and dirty way.

  • Just wanted to say thanks to @magnakai and @morgan for their solutions. Super useful!

    I had a number of field groups so wrote a function that loops over them. Presuming they are structured in the following manner, with a single $group per file.

    
    field-groups/
      field/
        data.php
      field2/
        data.php
    

    https://gist.github.com/thisislawatts/2364054d191d062a6f14b711714083eb

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

The topic ‘Convert PHP register_field_group array to JSON’ is closed to new replies.