Support

Account

Home Forums General Issues Safe way to include ACF and extension in theme

Solving

Safe way to include ACF and extension in theme

  • I need to include ACF along with the repeater, gallery, and options extensions in my theme that is installed on a Multisite environment where ACF and the extensions are also installed in the /plugins directory.

    How should I include them in the theme so that if the site admin erroneously enables the plugin it doesn’t generate a fatal error?

    /**
     * Add Advanced Custom Fields Plugin
     * (but hide the Admin UI...)
     */
    define( 'ACF_LITE' , true );
    if(!class_exists('acf')){
    	include_once('plugins/advanced-custom-fields/acf.php' );
    }
    if( !function_exists('acf_register_repeater_field') ){
    	include_once('plugins/acf-repeater/acf-repeater.php' );
    }
    if( !function_exists('acfgp_register_fields') ){
    	include_once('plugins/acf-gallery/acf-gallery.php');
    }
    if( !function_exists('register_options_page') ){
    	include_once('plugins/acf-options-page/acf-options-page.php');
    }
    include_once('inc/custom-fields.php');
    
    acf_add_options_sub_page(array(
        'title' => 'Custom Search',
        'parent' => 'options-general.php',
        'capability' => 'manage_options'
    ));

    The core ACF is fine since it is wrapped in a ‘if class doesn’t exist’ statement and the repeater and options are wrapped in a ‘if function doesn’t exits’ but the gallery is not.

  • Here’s how I set ACF within my plugins. Works well for me and doesn’t conflict when I have multiple plugins using ACF.

    // Check if ACF is used with another plugin, if not already called, use this one
    if( ! class_exists('acf') ) {
    	
    	// Load only limited functionality
    	define( 'ACF_LITE' , true );
    	
    	// Load the ACF Core
    	include_once( SET_PLUGIN_NAME_DIR . '/includes/acf/acf.php');
    	
    	// Repeater Add-On
    	if( ! class_exists('acf_repeater_plugin') ) {
    		include_once( SET_PLUGIN_NAME_DIR . '/includes/acf/add-ons/acf-repeater/acf-repeater.php');
    	}
    	
    	// Reapeater Collapser Add-on
    	if( ! class_exists( 'acf_repeater_collapser_assets' ) ) {
    		include_once( SET_PLUGIN_NAME_DIR . '/includes/acf/add-ons/acf-repeater-collapser/acf_repeater_collapser.php');
    	}
    
    	// Options Add-on
    	if( ! class_exists('acf_options_page_plugin') ) {
    		include_once( SET_PLUGIN_NAME_DIR . '/includes/acf/add-ons/acf-options-page/acf-options-page.php');
    
    		// Remove the defualt "Options" Page from ACF as it would be included within the plugin menu or elsewhere.
    		if( ! function_exists("remove_acf_options_page")) {
    			function remove_acf_options_page() {
    				remove_menu_page('acf-options');
    			}
    			add_action('admin_menu', 'remove_acf_options_page', 99);
    		}
    	}
    
    	// WYSIWYG Editor Add-on
    	if( ! class_exists( 'acf_field_wp_wysiwyg_plugin' ) ) {
    		include_once( SET_PLUGIN_NAME_DIR . '/includes/acf/add-ons/acf-wp-wysiwyg/acf-wp_wysiwyg.php');
    	}
    	
    	// Gallery Add-on
    	if( ! class_exists( 'acf_field_gallery' ) ) {
    		include_once( SET_PLUGIN_NAME_DIR . '/includes/acf/add-ons/acf-gallery/acf-gallery.php');
    	}
    
    	// Address Field Add-on
    	if( ! class_exists( 'acf_field_address_plugin' ) ) {
    		include_once( SET_PLUGIN_NAME_DIR . '/includes/acf/add-ons/acf-field-address/acf-address.php');
    	}
    	
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Safe way to include ACF and extension in theme’ is closed to new replies.