Preferred way to include Advanced Custom Fields in a plugin?

So I am building a plugin for a site that is already using Advanced Custom Fields (ACF). I wonder what would be the best way to include ACF in my plugin? The site is already using ACF, so is it possible to include ACF from the plugin directory or should I include ACF again?

Related posts

3 comments

  1. If ACF (or any other plugin) is active on the site you do not need to include its files as they are all being included in the wordpress initialization process. The only tricky part is that you don’t know the order in which files are included and yours might be included before the ACF files are included, therefor you should probably wait for the init action or even later action before using it.

    Side note: ACF provides GUI for custom fields, but if you just need to use the data it is better not to rely on its API and query using the get_post_meta API. This will result in better software modularization and hopefully a more maintainable site.

  2. This information has been updated!

    Please see ACF’s website:
    https://www.advancedcustomfields.com/resources/including-acf-in-a-plugin-theme/

    Well you would just check by if ACF class exists if( !class_exists('acf') ) to check if a user has ACF installed already and if not then, fall-back on your version of ACF you would included along with your plugin or theme, setting ACF_LITE to “not” show ACF menu to the user…

    But if a user decides to install ACF themselves, it will override your version included with you plugin or theme as expected :).

        // Optionally you can use below to check by plugin location:
        // if((is_plugin_active('advanced-custom-fields/acf.php'))|| (is_plugin_active('advanced-custom-fields-pro/acf.php'))):
            
    
            // Check if ACF Classes exists: 
            if(!class_exists('acf_pro') || !class_exists('acf')):
                
                // Define path and URL to the ACF plugin.
                define( 'MY_ACF_PATH', get_stylesheet_directory() . '/includes/acf/' );
                define( 'MY_ACF_URL', get_stylesheet_directory_uri() . '/includes/acf/' );
                
                // Include the ACF plugin.
                include_once( MY_ACF_PATH . 'acf.php' );
                
                // Customize the url setting to fix incorrect asset URLs.
                add_filter('acf/settings/url', 'my_acf_settings_url');
                function my_acf_settings_url( $url ) {
                    return MY_ACF_URL;
                }
                
                // (Optional) Hide the ACF admin menu item.
                add_filter('acf/settings/show_admin', 'my_acf_settings_show_admin');
                function my_acf_settings_show_admin( $show_admin ) {
                    return false;
                }
                
                
           endif;
    

    Also. After you have made your fields and field group with ACF, export them to PHP using ACF’s export feature. Then include that exported PHP into you plugin/theme.

    ACF is so powerful. And I am grateful for Elliott generosity of allowing us to even use PRO into making our plugins!

Comments are closed.