ZaZaKi, a web developer Between Manchester UK & Rotterdam NL. © 2015-2024.

WP Plugin Registration Hooks

 

Three hooks

  • register_activation_hook();
  • register_deactivation_hook();
  • register_uninstall_hook();

register_activation_hook();
When this function is activated register_activation_hook(); registers / adds options to the database (MySQL) see MySQL wp_option table search for (myplugin_posts_per_page)

// activation
function on_activation() {
// if the user has right ? otherwise return 	
if ( ! current_user_can( 'activate_plugins' ) ) return;

   add_option( 'myplugin_posts_per_page', 10 );
   add_option( 'myplugin_show_welcome_page', true );

}
register_activation_hook( __FILE__, 'on_activation' );

 

register_deactivation_hook();
deactivates the function but not remove it

// deactivation
function on_deactivation() {
// if the user has right ? otherwise return 	
if ( ! current_user_can( 'activate_plugins' ) ) return;

	flush_rewrite_rules();

}
register_deactivation_hook( __FILE__, 'on_deactivation' );

 

register_uninstall_hook();
Unstall the function remove it from database MySQL

 // uninstall
function on_uninstall() {
// if the user has right ? otherwise return 	
if ( ! current_user_can( 'activate_plugins' ) ) return;

	delete_option( 'myplugin_posts_per_page', 10 );
	delete_option( 'myplugin_show_welcome_page', true );

}
register_uninstall_hook( __FILE__, 'on_uninstall' );

Learn more about register_activation_hook():

https://codex.wordpress.org/Function_Reference/register_activation_hook
https://developer.wordpress.org/reference/functions/register_activation_hook/