Home Forums Feature Requests Anyway to store the JSON from PHP


Anyway to store the JSON from PHP

  • I have been reading up on ACF 5 and the new JSON storage seems Awesome! However, it only gets generated via the Admin Panel and not if I register fields via PHP.

    My Team have been only using PHP for that past year now as we use GIT for every project. Some of our projects have hundreds of registered fields so it would be awesome if we could utilize the JSON storage for speed, but get it generated from the PHP code.

    It would be nice if you could check it automatically for newly registered fields or maybe against a hash, etc.

    If not then allowing us to call a function would be fine too.
    Example. acf_generate_json_from_php();

    PS. We will always be using the php method as it is much faster for us and much more flexible so reverting back to the admin panel is not going to work.


  • This is the only reason why my team has not moved to acf 5 yet.
    We are fine with running function or something else to get this to work. So any help would be appreciated.


  • Hi @geoff-grav,

    It’s a good suggestion and I’m almost a little surprised it’s not already been done. Elliot probably hasn’t thought of it since both PHP and JSON solutions make it possible to version-control fields etc. One would have to figure out priority so that JSON is preferred to php based on a timestamp (just like DB vs JSON is).

    I’ll make sure Elliot sees this thread 🙂

    However I’m not sure why you can’t move to ACF5 anyway? If JSON storage is the only reason you still don’t have it in ACF4.x so moving to ACF5 would just mean better stuff for you and eventually (hopefully) a working php > JSON relationship. Upgrading is also completely non destructive so should something break you can just switch back to ACF4.x without loosing any data (But always do a backup anyway).

  • Thanks,

    We don’t have a need to move to ACF5 right now. I know there are a lot of nice features, but we don’t need them at the moment. However, our clients do mention often how slow the Admin Panel is. We have gotten around this a little by switching from Apache to Nginx only and using W3TC for DB and Object Caching, but it is still slow. So we are curious how much faster JSON would make it. Granted we understand that not everyone is using it like we are as we use Flexible Content to build most of the pages so some of our client have hundreds of fields on single page. It could take them up to 8-10 seconds to load before they can make any changes to the page.

  • I don’t have any benchmarking on testing with and without ACF JSON sync but it’d be very interesting to see the difference..

    If/when this is fixed and you do make a switch it’d be very nice if you post your results for us to see 🙂

    Another way to go about this for you (if you’re in pressing need) might be to:
    * Update to ACF 5
    * Create the acf-json folder in the theme
    * Import all the fields into field group admin
    * Delete the php fields
    * Use the JSON files for your version control etc. instead of continuing on php. Alternatively continue using PHP too but for live -pushes make sure to import them into field group admin first and skip pushing/uploading the php files to Prod.

  • Thanks,

    I am playing with ACF5 right now.
    Looking at the JSON it looks almost identical to the PHP files.

    So Here is the Big Question:
    ** Can’t you just load the fields with the PHP data instead of JSON ***

    All that would be needed for us would be to specify a load type:
    define(‘ACF_LOAD_TYPE’, ‘php’);

    Default would be:
    define(‘ACF_LOAD_TYPE’, ‘json’);

    By setting it to ‘php’ we agree that ACF will not look at the database and instead only use what we have defined with php.

  • Ok, after a bit of time I see that when using PHP it doesn’t store any fields in the database. So we may have already been getting the benefits of not having to hit the database when using php. Which is good, but it doesn’t address the issue of the Admin still being slow when loading Fields, specifically Flexible Content.

    So now the main question is:
    If we have already only been using PHP to build our fields do we benefit from using JSON as well?

    My initial thought is No, but we would need Elliot to confirm.
    I may as well run some bench marks to determine that.

    Also I have been playing with exporting fields from PHP to JSON.
    I will have a follow up on that soon.

  • Hi @geoff-grav

    The main benefit for JSON is the ability to sync the JSON data to and from the database, and also the ability to simply drop a JSON file into your theme’s acf-json folder.

    Sharing field groups between projects is a good example of why this is easier than PHP.

    performance should be similar between PHP and JSON, if neither had field groups in the database.

    If you are experiencing slowdown issues in the admin, this will most likely be due to HTML and JS, not PHP.

    Check your page source. How many lines of HTML are in the ‘slow’ admin page?
    Next, disable JS. What is the time difference when JS in enabled/disabled?

    If the issue is JS, perhaps you have some complex conditional logic? This can slow down your page load if using large amounts of nested fields. The latest ACF PRO contains some JS performance improvements, but this is simply a limitation of our browsers at the current time.


  • Thanks Elliot,
    Well after a bunch of testing I finally found the main culprit.

    First off, it seems that JSON does not have a speed advantage over PHP.
    Not in my tests anyway. Which for me is good as I want stick with PHP.

    However, I did test upgrading to ACF5 and that increased the Speed drastically.
    Page load went from 15sec to 2sec.

    So I started looking as to why.
    it seems it mainly had to do with the “page-link” field.
    In ACF4 this field prints out the Select field and all the options. This is normal, but because the client has a few hundred posts, few hundred pages, products, casestudies, etc. The Select dropdowns were outputing about 1,000 option fields. On Top of that we are using Flexible Content and therefore there can be quite a few “page-links” on the page. This was causing the post.php file to be 4.4mb. This was making the page load extremely slow. In ACF5 you have switched this out for an Ajax Dropdown. So no more thousands of options in select fields. This took the page size down to 974kb.

    Even though this helped a lot I would suggest looking into ways to reduce the amount of markup. It seems something that my team can do is reduce the size of the key fields, but I would ask that you look into reducing the markup.

    This seems quite long for an id:
    I know it needs to be unique, but maybe you can hash it or something to reduce the size as this is also used in the “for” attribute. Maybe also look into reducing the amount of tags, white-space, etc.

    To get around this issue in ACF4 you would need to change your “page-links” to “text” fields and have the client input the URL or have them input the Post ID. This is not ideal, but it would decrease the load on the page.

    Because of this our team will look at incorporating ACF5 much sooner.

    There are some minor issues with it like the new Dropdowns show the entire post title. My clients use really long titles. This makes the admin all wonky as items don’t line up. I am sure I can work around this with a CSS fix, but I will post this in another Thread.


    Also if anyone needs this info here is how I found converting PHP into JSON:

    foreach (acf_get_local_field_groups() as $group)
    	$group['fields'] = acf_get_local_fields($group['key']);
    	echo json_encode($group, JSON_PRETTY_PRINT);
Viewing 11 posts - 1 through 11 (of 11 total)

The topic ‘Anyway to store the JSON from PHP’ is closed to new replies.