Support

Account

Home Forums ACF PRO Updates & ACF PRO distribution Reply To: Updates & ACF PRO distribution

  • I’ve found that with anything WP it’s a matter of understanding the order that things happen and when specific files are loaded that’s most important.

    For example, registering a field group in a plugin. The documentation for ACF says to put something like this in your functions.php file:

    
    if (function_exists('acf_add_local_field_group')) {
      // register your field group here
    }
    

    This works for 95% of users because for the average user they are going to be doing the work as part of a theme, and the documentation is aimed at what most people are going to do.

    But if you build a plugin and want to add fields groups in code the chances are good that the field groups will not get registered because ACF isn’t loaded when your plugin code is loaded. So you need to use an action hook that will not trigger until after all the plugins are loaded. There are several WP hooks that can be used. This could be done no the plugins_loaded hook or the init hook.

    
    add_action('init', 'register_my_field_groups');
    function register_my_field_groups() {
      if (function_exists('acf_add_local_field_group')) {
        // register your field group here
      }
    }
    

    or you can eliminate the need to check if the function exists by using an acf hook that will only be called if acf is actually active. (I actually don’t think this is documented anywhere, but I’ve spend a lot of hours looking at what ACF does and how it does it.)

    
    add_action('acf/include_fields', 'register_my_field_groups');
    function register_my_field_groups() {
      // register your field groups
    }
    

    It’s always hard to say what the best or most proper way to do anything is. I’ve been working with WordPress and ACF for a very long time and I still learn something new every time I build a site.

    The best thing to do is ask here and someone that’s been working with ACF for a while will probably pop in and try to help.