Created
June 23, 2021 05:40
-
-
Save kingjmaningo/847a0f0b6220c13f9f4f682b81af4a85 to your computer and use it in GitHub Desktop.
Plugin activated/deactivated hooks sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Let's assume this is your main plugin file | |
register_activation_hook( __FILE__, array( 'YOURTEXTDOMAIN', 'function_to_call_upon_activation' ) ); | |
//register_activation_hook( __FILE__, array ( $my_class, 'function_to_call') ); // If inside a Class | |
function function_to_call_upon_activation() { | |
// Check PHP Version and deactivate & die if it doesn't meet minimum requirements. | |
if ( version_compare( PHP_VERSION, '5.4', '<=' ) ) { | |
deactivate_plugins( plugin_basename( __FILE__ ) ); | |
add_action( 'admin_notices', 'my_plugin_notice' ); | |
//wp_die( __( 'This plugin requires PHP Version 5.4 or greater. Sorry about that.', 'textdomain' ) ); | |
} | |
// Do other stuff here | |
} | |
register_deactivation_hook( __FILE__, array( 'YOURTEXTDOMAIN', 'function_to_call_upon_deactivation' ) ); | |
function function_to_call_upon_deactivation() { | |
// require woocommerce | |
if ( is_admin() && current_user_can( 'activate_plugins' ) && !class_exists( 'woocommerce' ) ) { | |
add_action( 'admin_notices', 'my_plugin_notice' ); | |
deactivate_plugins( plugin_basename( __FILE__ ) ); | |
} | |
// Do other stuff here | |
} | |
function my_plugin_notice() { | |
?><div class="error"><p><?php esc_html_e('Error notice text here', GBSBOOKING_TEXTDOMAIN) ?></p></div><?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment