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/
10 Related pages
- WordPress Plugin Documentation
- WP Plugin Admin Main and Sub Menus
- WP Plugin boilerplate generator
- WP Plugin enable debug
- WP Plugin Must-Use Plugins
- WP Plugin Pluggable Functions
- WP Plugin prevent direct access to PHP code in WordPress
- WP Plugin Registration Hooks
- WP Plugin Secure WordPress Plugins
- WP Plugin WordPress APIs