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.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
Plugin boilerplates can do some of the heavy lifting during initial development. We look at four options to speed up your plugin creation. https://t.co/ZtMsdBxAHw
— Advanced Custom Fields (@wp_acf) June 5, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.