-
On activation , plugins can run a routine to add rewrite rules, add custom database tables, or set default option values. -
On deactivation , plugins can run a routine to remove temporary data such as cache and temp files and directories.
Activation
register_activation_hook( __FILE__, 'pluginprefix_function_to_run' );
Deactivation
register_deactivation_hook( __FILE__, 'pluginprefix_function_to_run' );
Example
/** * Register the "book" custom post type */ function pluginprefix_setup_post_type() { register_post_type( 'book', ['public' => true ] ); } add_action( 'init', 'pluginprefix_setup_post_type' ); /** * Activate the plugin. */ function pluginprefix_activate() { // Trigger our function that registers the custom post type plugin. pluginprefix_setup_post_type(); // Clear the permalinks after the post type has been registered. flush_rewrite_rules(); } register_activation_hook( __FILE__, 'pluginprefix_activate' );
/** * Deactivation hook. */ function pluginprefix_deactivate() { // Unregister the post type, so the rules are no longer in memory. unregister_post_type( 'book' ); // Clear the permalinks to remove our post type's rules from the database. flush_rewrite_rules(); } register_deactivation_hook( __FILE__, 'pluginprefix_deactivate' );
-
register_activation_hook() in the WordPress function reference. -
register_deactivation_hook() in the WordPress function reference.