How to Include ACF in a Plugin or Theme

Last updated Nov 29, 2023

Overview

To include ACF or ACF PRO within your plugin or theme, first make sure you are following the rules and conditions, and then download the appropriate ACF plugin files and copy them into your plugin or theme.

We recommend using the folder includes/acf within your codebase.

Next, use the following code as a starter to customize and include the ACF plugin in your plugin or theme.

Detecting if the ACF Plugin Is Installed

A user of your plugin or theme might also be using ACF for their site build. In that case, you should not proceed with bundling ACF or ACF PRO if the plugin is detected.

Use the following code to check if ACF or ACF PRO is installed:

if ( is_plugin_active( 'advanced-custom-fields/acf.php' ) || is_plugin_active( 'advanced-custom-fields-pro/acf.php' ) ) {
    return;
}

If the ACF or ACF PRO plugin is detected, you should not proceed with the following instructions.

Bootstrap the Bundled ACF

You will need to tell your plugin or theme to load the ACF files from where they are stored in your codebase:

// 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;
}

Disabling the ACF Admin

Your users should not be able to manage ACF field groups, fields, or options pages. Therefore you are required to hide the ACF admin pages and menu using the following code:


// Hide the ACF admin menu item. add_filter('acf/settings/show_admin', '__return_false');

Including Fields Definitions

Along with the plugin files itself, you will also need to include the field group and option pages definitions. We recommend either registering your field groups with PHP, or including a local JSON folder of the JSON file exports of your field groups and option pages.