Skip to content

Instantly share code, notes, and snippets.

@DevWael
Last active July 29, 2019 09:59
Show Gist options
  • Save DevWael/28312212f30474075a18e89e9141b955 to your computer and use it in GitHub Desktop.
Save DevWael/28312212f30474075a18e89e9141b955 to your computer and use it in GitHub Desktop.
use the following secured code in your plugin to run some logic on plugin activation, deactivation or uninstall events
<?php
//use the following code in your plugin to run some logic on plugin activation, deactivation or uninstall events
register_activation_hook( __FILE__, 'prefix_plugin_activate' );
function prefix_plugin_activate() {
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
$plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
check_admin_referer( "activate-plugin_{$plugin}" );
//your code goes here
}
register_deactivation_hook( __FILE__, 'prefix_plugin_deactivate' );
function prefix_plugin_deactivate() {
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
$plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
check_admin_referer( "deactivate-plugin_{$plugin}" );
//your code goes here
}
register_uninstall_hook( __FILE__, 'prefix_clean_wp_on_uninstall' );
function prefix_clean_wp_on_uninstall() {
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
check_admin_referer( 'bulk-plugins' );
if ( __FILE__ != WP_UNINSTALL_PLUGIN ) {
return;
}
//your code goes here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment