Last active
December 19, 2022 21:23
-
-
Save pablo-sg-pacheco/7b92acd6bed3c22ded38c9c9811d1c3b to your computer and use it in GitHub Desktop.
Custom deactivation/activation hooks from a WordPress plugin that can be called from some other hooks
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 | |
// Custom deactivation/activation hooks. | |
$activation_hook = 'plugin_prefix_on_activation'; | |
$deactivation_hook = 'plugin_prefix_on_deactivation'; | |
register_activation_hook( __FILE__, function () use ( $activation_hook ) { | |
add_option( $activation_hook, 'yes' ); | |
} ); | |
register_deactivation_hook( __FILE__, function () use ( $deactivation_hook ) { | |
do_action( $deactivation_hook ); | |
} ); | |
add_action( 'admin_init', function () use ( $activation_hook ) { | |
if ( is_admin() && get_option( $activation_hook ) === 'yes' ) { | |
delete_option( $activation_hook ); | |
do_action( $activation_hook ); | |
} | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The custom activation hooks can be called from some other hooks like
plugins_loaded
orinit
. Example: