Store Advanced Custom Field Local JSON in a plugin

Do you use the ACF Local JSON feature? You should!

Only bad part is, tying custom fields to a theme isn’t optimal, which I’ve spoken about earlier.

Here’s a quick way to put your ACF JSON field groups inside a plugin!

<?php
/*
Plugin Name: ACF Local JSON plugin
Plugin URI: 
Description: Put this file in a plugin folder and create an /acf directory inside the plugin, ie: /my-plugin/acf
Author: khromov
Version: 0.1
*/

//Change ACF Local JSON save location to /acf folder inside this plugin
add_filter('acf/settings/save_json', function() {
    return dirname(__FILE__) . '/acf';
});

//Include the /acf folder in the places to look for ACF Local JSON files
add_filter('acf/settings/load_json', function($paths) {
    $paths[] = dirname(__FILE__) . '/acf';
    return $paths;
});

After you create the main plugin file, make sure to create an empty folder called acf as well in the plugin root – that’s where your local JSON files will be saved.

8 thoughts on “Store Advanced Custom Field Local JSON in a plugin

  1. Joe Fletcher

    Hey Stan,

    I just put your acf-json snippet in my plugin, and it works like a charm. Thanks! Along with ACF’s json sync back into the db, this really helps with using ACF within a plugin for option fields, etc.

    Reply
  2. Ryan

    Could this solution be used to share custom fields across all of the subsites in a WordPress Multisite network?

    Reply
    1. Stanislav Khromov Post author

      Absolutely. We’re doing this on a project right now. Just make sure to network activate your plugin / ACF and you’re golden!

      Reply
  3. Mark

    Note that this will not play well with others doing the same thing due to acf/settings/save_json only allowing a single location for saved JSON. If you’re distributing this and concerned about compatibility, you may want to only include the second hook and not the first.

    Reply
    1. Stanislav Khromov Post author

      You are completely correct Mark.

      I’ve just started working on a plugin that’s called “Local JSON Manager” that would let multiple plugin register their ACF save_json folders and the user would then get a dropdown menu in the admin bar where they could select which folder should be used for saving. If that sounds interesting and you want to collab, shoot me a message at [email-obfuscate email="stanislav.khromov@gmail.com"]

      Reply
  4. Colin

    Awesome! Needed this for multisite and it works perfectly. Probably will use it on all my sites as I am already storing ACF in a custom plugin. Only makes sense to have the json stay with it.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Markdown is allowed in comments.