Support

Account

Home Forums ACF PRO Including ACF in a Plugin… caveats??

Solved

Including ACF in a Plugin… caveats??

  • Hello!

    I am including ACF in a plugin. However, I don’t want to assume that the web site does not already have ACF in use (which I think that I addressed below)… and I don’t want to assume that it’ll never be in use. And… how about versions… what if they *do* have the *free* version, then what?

    Here’s the code that I am using:

    <?php
    if( ! class_exists('acf') ) {
      function keith_acf_settings_path( $path ) {
        $path = plugin_dir_path( __FILE__ ) . 'acf/';
        return $path;
      }
      add_filter('acf/settings/path', 'keith_acf_settings_path');
      function keith_acf_settings_dir( $dir ) {
        $dir = plugin_dir_url( __FILE__ ) . 'acf/';
        return $dir;
      }
      add_filter('acf/settings/dir', 'keith_acf_settings_dir');
      include_once( plugin_dir_path( __FILE__ ) . 'acf/acf.php' );
      // hide ACF field group menu item
      add_filter('acf/settings/show_admin', '__return_false');
      function keith_acf_json_save_point( $path ) {
        $path = plugin_dir_path( __FILE__ ) . 'acf-data';
        return $path;
      }
      add_filter('acf/settings/save_json', 'keith_acf_json_save_point');
      function keith_acf_json_load_point( $paths ) {
        unset($paths[0]);
        $paths[] = plugin_dir_path( __FILE__ ) . 'acf-data';
        return $paths;
      }
      add_filter('acf/settings/load_json', 'keith_acf_json_load_point');
      @chmod(plugin_dir_path( __FILE__ ) . 'acf-data', 0755);
    }
    ?>

    Here are my questions:

    1) It *seems* like the above should work if they do not currently have ACF installed, is that right? In my testing, it seems fine.

    2) With the code above, if the user ends up starting to use standalone-ACF after the fact, then the data retrieval will suddenly shift to the DB rather than JSON… so… it seems my custom fields will be lost.. yes? Is there a change that I could make to my code to account for that situation?

    3) I take care to name my custom fields very uniquely… but I see that the JSON data has internal field names. That could potentially cause conflicts if someone started to use standalone-ACF, couldn’t it? How could I account for that?

    4) Is there a better conditional statement that I could use? I mean… I use ACF Pro and many of the features that do not exist in the Free version. What if someone has the free version already installed, or installs it later… is my code going to break?

    I think that’s it. I believe I have thought of every reasonable scenario. Let me know your thoughts please. Greatly appreciated!

    Keith

  • If they do not have acf installed, then there shouldn’t be problems

    If the site has ACF installed as a stand alone plugin, then there will likely be problems. If they use the free version then this will definitely cause conflicts. ACF4 and 5 work differently and store data for field groups and field settings in completely different ways. The order that plugins are installed will determine Which one will be used and there isn’t any way to determine the order. Basically, from my experience, plugins are loaded in the order that they were activated, and this order changes every time that a plugin is deactivated and activated again and also changes every time a plugin is updated.

    Basically, you will need to have some type of checking to see what version of ACF is running and if it is not 5 then you’ll, well, I’m not sure. Your plugin will probably not work, or the stand alone ACF4 will not work determined by which is loaded first.

    I know that ACF is built so that you can include it in plugins and themes, but I’ve never been a fan of including other plugins in my plugins and themes because of the many problems that you need to overcome. This is particularly true of extremely popular plugins like ACF. You may want to include something in your documentation that ACF5 is used and required and maybe provide some type of configuration setting, to conditionally load your included version or the stand alone installed separately.

    You might also create a “plugins_loaded” action that only loads the version in your plugin if ACF is not already active. In the same action you could detect the version of ACF and if it’s not 5 you can automatically deactivate your plugin with some type of error message saying that it was deactivated and that ACF5 is required or something.

    Not sure if any of this will help you.

  • I’m very interested in knowing how to avoid conflicts like this as well. I’m looking into using ACF Pro in a theme, but do not want to risk customers already having ACF Pro or free ACF activated on their site.

    Looking forward to an update!

  • In the case of ACF installed in a theme, if a stand alone ACF plugin is installed then it will always be used. Plugins are loaded before the theme. There isn’t any 100% effective way to avoid conflicts with 2 versions of ACF that I know of. This is true of any plugin. If someone comes up with a solution, other than not including ACF in a them or plugin and requiring that it be installed separately I’d be interested in hearing it.

  • The answer is… that it’s silly promoting the use ACF in themes and plugins unless you are in a vaccuum 😉 I was soooo excited about the possibilities. It was a mere pipe dream lol Still love and appreciate the tool though. Bet ya Elliot is kicking himself for not charging yearly for it!

  • @klock I agree with this. I run into problems constantly with plugin compatibility not just with ACF, but keeping this to just ACF, I have plugins that depend on ACF Pro and I once installed a theme that required the standard version of ACF be installed, if it wasn’t the theme would just keep reinstalling it. If I had known that the theme required ACF4 and would not run without it installed I never would have considered it as a possible solution for the client. Worse than including it in a theme or a plugin is not making that information available for those that might use it.

    Hopefully, the ACF4/5 problem will be solved soon as E has said a free version of 5 should be released soon.

  • Cool!

    You know what would be awesome…

    … if we could use ACF (any version) in our plugin/theme, and keep or fields tucked away in JSON…

    … and then, even if someone already has ACF installed (any version), or installs it later..

    … our plugin/theme hums along nicely because we are just running a unique “instance” of ACF.

    Isn’t that how classes work in PHP? I’m limited in what I know about programming.

    Anyway… I realize it probably wasn’t a priority in the early days to make it so we can use the plugin in our own work. However, I would happily pay a yearly fee for that ability.

    I have heaps of plugins created on individual websites using ACF. I would love to package them up and put them on wp.org or sell the ones that use ACF Pro.

    I gave up on that idea awhile back though, and it’s just too cumbersome to *not* use ACF.

  • I agree, it’s unusal that ACF would be promoted as a plugin or theme development tool when it so easily can have conflicts from separate installations of the plugin. What’s the point of including it in my theme or plugin then? Oh well.

    I also thought it would be nice if it’s possible to use ACF to build a theme or plugin, then remove it from that theme or plugin and utilize some sort of JSON export in my theme without the need for ACF to be included. Though, that doesn’t really sound possible even with my severe lack of programming knowledge.

  • The problem is not including it in the theme or a plugin, the problems start when there are different versions included. This only works if everyone includes the same version. This can be said for any library that could be included in a theme or plugin.

    Right now there is a compatibility problems with ACF4 and 5, but there would also be a compatibility problem with 5.4 vs 5.5 (introduction of term meta to ACF)

    The same problems happen when 2 plugins provide different versions of Select2 or Font Awesome or for some reason load a different version of jQuery than is provided by WP. The problem is not with ACF, the problem is that if you want to do this you need to keep it updated and you need to depend on every other developer that’s using it to do the same.

  • Thanks @hube2 that makes sense. But can you clarify a couple things for me?

    When you say “different versions are included” do you mean when I include v5 built into my theme, but then the customer decides to install v4 from the plugin repository? That’s the scenario I’m worried about.

    So with that in mind, it’s not very safe for me to build a theme or a plugin that has ACF built in (included). I can’t account for every customer scenario and hope they don’t install another version of ACF.

    What would be great is if ACF had a way to auto detect when there is a conflicting version of ACF installed in addition to itself. Then display a dashboard warning telling the user they have a plugin that could possibly conflict with theme (or plugin) that I made. This way the user is aware of what’s going on. Unfortunately I don’t have the knowledge to build that detection functionality myself, but it might be a good solution to this conflict problem.

    Thanks!

  • Right now, yes, the main problem is having ACF4 and 5 both active and they are not compatible with each other. That should be resolved, hopefully soon. I don’t know Elliot’s timetable for getting ACF5 released to the WP repo, but I’m hoping that it’s very soon.

    You’re theme can check the version of ACF that is active and display an admin notice

    
    add_action('init', 'my_theme_check_acf_version');
    function my_theme_check_acf_version() {
      $acf_version = acf_get_setting('version');
      if (version_compare($acf_version, '5.0.0', '<')) {
        // the version above could be changed to whatever
        // version your theme requires
        add_action('admin_notices', 'my_admin_notices_function');
      }
    }	
    

    https://codex.wordpress.org/Plugin_API/Action_Reference/admin_notices

  • Thanks for clearing that up! I really appreciate it. And thanks for the code!

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

The topic ‘Including ACF in a Plugin… caveats??’ is closed to new replies.