Useful Snippets

Welcome!


This blog is used to collect useful snippets related to Linux, PHP, MySQL and more. Feel free to post comments with improvements or questions!

Are your smart devices spying on you? Make better purchasing choices and find products that respect your privacy at Unwanted.cloud

RSS Latest posts from my personal blog


Subscribe to RSS feed


Store Advanced Custom Field Local JSON in a plugin

Stanislav KhromovStanislav Khromov

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.

Full-stack impostor syndrome sufferer & Software Engineer at Schibsted Media Group

Comments 8
  • Joe Fletcher
    Posted on

    Joe Fletcher Joe Fletcher

    Reply Author

    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.


  • Ryan
    Posted on

    Ryan Ryan

    Reply Author

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


    • Stanislav Khromov
      Posted on

      Stanislav Khromov Stanislav Khromov

      Reply Author

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


  • Mark
    Posted on

    Mark Mark

    Reply Author

    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.


    • Stanislav Khromov
      Posted on

      Stanislav Khromov Stanislav Khromov

      Reply 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"]


    • Stanislav Khromov
      Posted on

      Stanislav Khromov Stanislav Khromov

      Reply Author

      There is now a plugin available to work with multiple plugins and themes using local JSON called the ACF Local JSON Manager:
      https://github.com/khromov/acf-local-json-manager


      • Koen
        Posted on

        Koen Koen

        Reply Author

        Great solution, Stanislav. Thanks!


  • Colin
    Posted on

    Colin Colin

    Reply Author

    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.