Skip to content

Instantly share code, notes, and snippets.

@bradmkjr
Last active October 9, 2018 17:55
Show Gist options
  • Save bradmkjr/5fb3b6749ad7654c6eb8257aade40749 to your computer and use it in GitHub Desktop.
Save bradmkjr/5fb3b6749ad7654c6eb8257aade40749 to your computer and use it in GitHub Desktop.
Require ACF
<?php
/*** Require ACF plugin ***/
//Check if ACf exists on plugin activation
register_activation_hook( __FILE__, 'acf_plugin_activation' );
function acf_plugin_activation(){
// Require ACF or ACF Pro plugin
if ( ( !is_plugin_active( 'advanced-custom-fields/acf.php' ) && !is_plugin_active( 'advanced-custom-fields-pro/acf.php' ) ) and current_user_can( 'activate_plugins' ) ) {
// Stop activation redirect and show error
wp_die('Sorry, but this plugin requires the ACF or ACF Pro Plugin to be installed and active. <br><a href="' . admin_url( 'plugins.php' ) . '">&laquo; Return to Plugins</a>');
}
}
//Check if ACF or ACF Pro is installed and activated
add_action( 'admin_init', 'acf_plugin_check' );
function acf_plugin_check() {
if ( is_admin() && current_user_can( 'activate_plugins' ) && !is_plugin_active( 'advanced-custom-fields/acf.php' ) && !is_plugin_active( 'advanced-custom-fields-pro/acf.php' ) ) {
add_action( 'admin_notices', 'acf_plugin_notice' );
deactivate_plugins( plugin_basename( __FILE__ ) );
if ( isset( $_GET['activate'] ) ) {
unset( $_GET['activate'] );
}
}
}
//User notice that ACF is required
function acf_plugin_notice(){
?><div class="error"><p>Sorry, but Plugin requires the ACF or ACR Pro plugin to be installed and active.</p></div><?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment