Home › Forums › General Issues › Include ACF to my plugin
Yes, i lost three hours to find the way to do include ACF-PRO to my plugin. Now, i am testing in localhost. I read and follow documention at
https://www.advancedcustomfields.com/resources/including-acf-in-a-plugin-theme
I put the ACF source in a folder called “acf”, and put this codes of documention in my main plugin file:
<?php
// 1. customize ACF path
add_filter('acf/settings/path', 'my_acf_settings_path');
function my_acf_settings_path( $path ) {
// update path
$path = get_stylesheet_directory() . '/acf/';
// return
return $path;
}
// 2. customize ACF dir
add_filter('acf/settings/dir', 'my_acf_settings_dir');
function my_acf_settings_dir( $dir ) {
// update path
$dir = get_stylesheet_directory_uri() . '/acf/';
// return
return $dir;
}
// 4. Include ACF
include_once( get_stylesheet_directory() . '/acf/acf.php' );
Then i try to load my test site, it returned an error that cannot find the file.
Image Error1
I try to repalce this line include_once( get_stylesheet_directory() . '/acf/acf.php' );
by
include_once( plugin_dir_path(__FILE__) . '/acf/acf.php' );
The result is it work, but ACF can not load it’s styles, image below:
I found for some post like my happen, but can not find the way to fix this. I just learning about PHP and WordPress, so i don’t have enough knowledge. Please help me!
Thanks a lot!
Here’s the code that I use at the top of my plugins where I include ACF Pro, and it works flawlessly. Hope it helps you!
if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/** Start: Detect ACF Pro plugin. Include if not present. */
if ( !class_exists('acf') ) { // if ACF Pro plugin does not currently exist
/** Start: Customize ACF path */
add_filter('acf/settings/path', 'cysp_acf_settings_path');
function cysp_acf_settings_path( $path ) {
$path = plugin_dir_path( __FILE__ ) . 'acf/';
return $path;
}
/** End: Customize ACF path */
/** Start: Customize ACF dir */
add_filter('acf/settings/dir', 'cysp_acf_settings_dir');
function cysp_acf_settings_dir( $path ) {
$dir = plugin_dir_url( __FILE__ ) . 'acf/';
return $dir;
}
/** End: Customize ACF path */
/** Start: Hide ACF field group menu item */
add_filter('acf/settings/show_admin', '__return_false');
/** End: Hide ACF field group menu item */
/** Start: Include ACF */
include_once( plugin_dir_path( __FILE__ ) . 'acf/acf.php' );
/** End: Include ACF */
/** Start: Create JSON save point */
add_filter('acf/settings/save_json', 'cysp_acf_json_save_point');
function cysp_acf_json_save_point( $path ) {
$path = plugin_dir_path( __FILE__ ) . 'acf-json/';
return $path;
}
/** End: Create JSON save point */
/** Start: Create JSON load point */
add_filter('acf/settings/load_json', 'cysp_acf_json_load_point');
/** End: Create JSON load point */
/** Start: Stop ACF upgrade notifications */
add_filter( 'site_transient_update_plugins', 'cysp_stop_acf_update_notifications', 11 );
function cysp_stop_acf_update_notifications( $value ) {
unset( $value->response[ plugin_dir_path( __FILE__ ) . 'acf/acf.php' ] );
return $value;
}
/** End: Stop ACF upgrade notifications */
} else { // else ACF Pro plugin does exist
/** Start: Create JSON load point */
add_filter('acf/settings/load_json', 'cysp_acf_json_load_point');
/** End: Create JSON load point */
} // end-if ACF Pro plugin does not currently exist
/** End: Detect ACF Pro plugin. Include if not present. */
/** Start: Function to create JSON load point */
function cysp_acf_json_load_point( $paths ) {
$paths[] = plugin_dir_path( __FILE__ ) . 'acf-json-load';
return $paths;
}
/** End: Function to create JSON load point */
Very good idea, Keith. I bundeled this into a separate class that I can easily use in the plugin boilerplate. Assuming you have the ACF files in a folder “acf” in a directory “lib”:
<?php
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) )exit;
/**
* Integrate ACF if needed
*
* @author André R. Kohl <[email protected]>
*/
class ss_acf_integrate {
/**
* The unique identifier of this plugin.
*
* @since 1.0.0
* @access protected
* @var string $plugin_name The string used to uniquely identify this plugin.
*/
protected $plugin_name = '';
/**
* The current version of the plugin.
*
* @since 1.0.0
* @access protected
* @var string $version The current version of the plugin.
*/
protected $version = '1.0.0';
/**
* The modus of ACF: Either "installed" if found as a plugin, "bundeled" when used via include ore false if not found
*
* @since 1.0.0
* @access public static
* @var string $acf_modus The used modus.
*/
public static $acf_modus;
/**
* The path to the bundeled ACF
*
* @since 1.0.0
* @access protected
* @var string $acf_dir The path to the folder.
*/
protected $acf_dir;
/**
* The URL to the bundeled ACF
*
* @since 1.0.0
* @access protected
* @var string $acf_url The url to the folder.
*/
protected $acf_url;
/**
* The path to the json files
*
* @since 1.0.0
* @access protected
* @var string $acf_json The path to the folder.
*/
protected $acf_json;
/**
* Initialize the class and set its properties.
*
* @since 1.0.0
* @var string $name The name of this plugin.
* @var string $version The version of this plugin.
*/
public function __construct( $name = false, $version = false ) {
if($name){
$this->plugin_name = $name;
}
if($version){
$this->plugin_version = $version;
}
$this->acf_dir = plugin_dir_path( dirname( __FILE__ ) ) .'lib/acf/';
$this->acf_url = plugin_dir_url( dirname( __FILE__ ) ) .'lib/acf/';
$this->acf_json = plugin_dir_path( dirname( __FILE__ ) ) .'lib/acf_json/';
if(is_dir ( $this->acf_json )) {
if(!is_writable($this->acf_json )){
chmod($this->acf_json , '777');
}
} else {
mkdir($this->acf_json, 0777, true);
}
if ( class_exists('acf') ){
self::$acf_modus = 'installed';
} else if(file_exists($this->acf_dir.'acf.php')) {
self::$acf_modus = 'bundeled';
} else {
self::$acf_modus = false;
}
$this->init();
}
/**
* Initiate the integration
*
* @since 1.0.0
*/
private function init(){
if(!self::$acf_modus){
return;
}
if ( 'bundeled' === self::$acf_modus ) {
// Customize ACF path
add_filter('acf/settings/path', array($this, 'acf_settings_path'));
// Customize ACF URL
add_filter('acf/settings/dir', array($this, 'acf_settings_dir'));
// Stop ACF upgrade notifications
add_filter( 'site_transient_update_plugins', array($this, 'stop_acf_update_notifications' ), 11);
// Create JSON save point
add_filter('acf/settings/save_json', array($this,'acf_json_save_point'));
// Include ACF
require_once( $this->acf_dir . 'acf.php' );
}
// Show/Hide ACF admin based on config
if(defined('ACF_SHOW_ADMIN') && false === ACF_SHOW_ADMIN){
add_filter('acf/settings/show_admin', '__return_false');
}
// Create a json load point
add_filter('acf/settings/load_json', array($this, 'acf_json_load_point'));
}
/**
* Filters the path to the ACF folder
*
* @since 1.0.0
*/
public function acf_settings_path( $path ) {
$path = $this->acf_dir ;
return $path;
}
/**
* Filters the URL to the ACF folder
*
* @since 1.0.0
*/
public function acf_settings_dir( $path ) {
$path = $this->acf_url ;
return $path;
}
/**
* Stops the upgrade notifications of ACF
*
* @since 1.0.0
*/
public function stop_acf_update_notifications( $value ) {
unset( $value->response[ $this->acf_dir . 'acf.php' ] );
return $value;
}
/**
* Creates a json load point
*
* @since 1.0.0
*/
public function acf_json_load_point( $paths ) {
$paths[] = $this->acf_json_load;
return $paths;
}
/**
* Creates a json save point
*
* @since 1.0.0
*/
public function acf_json_save_point( $path ) {
$path = $this->acf_json_save;
return $path;
}
/**
* Returns the current value of acf_modus for use in plugins or themes
*
* @since 1.0.0
*/
public static function acf_modus() {
return self::$acf_modus;
}
}
Call this class in the function load_dependencies() like this:
/**
* The class for integrating ACF
*/
if ( !class_exists( 'ss_acf_integrate ' ) ) {
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-ss_acf_integrate.php';
new ss_acf_integrate( $this->get_plugin_name(), $this->get_version() );
}
I decided to have a little switch to enable/disable the ACF admin. In your wp-config.php add
// ACF Admin
define ('ACF_SHOW_ADMIN', true);
where obviously true means show the ACF admin and false means hide it.
Thanks again for your fantastic starting point.
A little error in the ss_acf_integrate class: Change these two:
/**
* Creates a json load point
*
* @since 1.0.0
*/
public function acf_json_load_point( $paths ) {
$paths[] = $this->acf_json;
return $paths;
}
/**
* Creates a json save point
*
* @since 1.0.0
*/
public function acf_json_save_point( $path ) {
$path = $this->acf_json;
return $path;
}
i am missing the “perfect” solution by ACF, but thanks for your way to do it in the right direction.
I want to include it in the boilerplate too and “feel” that it could be not the perfect way. I used your direction in the start of my plugin development but now i try to build it new from a blank template and want to do it “in a perfect way”. Your direction is good but i missed the point to create the needed fields – in my old version i add a function “create fields” and called this in boilerplate admin hooks – i think there must be a better solution? Is this right?
Calling the creating fields function in init was not possible for me because some included functions havent worked for me and so i called it in the admin hooks but where is the better solution?
I miss a guide from ACF for perfect integration. They want to sell the plugin and should do a little bit more for the developers.
The topic ‘Include ACF to my plugin’ is closed to new replies.
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.