YogeshChauhan . com

How to activate and deactivate hooks in a WordPress plugin?

in WordPress on June 3, 2021

What are activation and deactivation hooks?

They are just hooks that helps perform different actions when WordPress plugins are activated or deactivated by the user.

There are few things to check or perform when a user activates a plugin. Those are routine tasks such as adding rewrite rules into .htaccess (or just in PHP file), add or update some data into a database, set some default option values for the user etc.

Same way we update or remove data and perform some other tasks when removing a plugin. The removable data include any temporary data saved by the plugin such as cache files, temp files or even directories.

Activation Hook

You can use register_activation_hook() function to set up an activation hook.

Just like this:

register_activation_hook( __FILE__, 'function_to_run' );

Deactivation Hook

You can use register_deactivation_hook() function to set up an activation hook.

Just like this:

register_deactivation_hook( __FILE__, 'function_to_run' );

The ‘__FILE__’ refers to the main plugin file.

The main plugin file is the one with plugin header comment and usually you keep these two functions inside the main file.

What if the main file doesn’t have those functions?

You just need to change the first parameter in those functions and point it to where the main plugin file is.

Example form WordPress Developers

This activation hook refreshes permalinks and get rid of 404 errors when you register a custom post type.

The deactivation hook just reverses the process.

 * 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.
  // Clear the permalinks after the post type has been registered.
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.
register_deactivation_hook( __FILE__, 'pluginprefix_deactivate' );


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

Learn how to add Scroll Indicator using CSS and JavaScript?CSSHow to convert datetime to date format in JavaScript?JavaScriptWhat is Hadoop and Hadoop Ecosystem?MiscKilling A Project Part 1: What criteria should be used to cancel/kill a project?MiscHow to remove N/A from Radio Button list in Drupal?DrupalHow to show and hide an element on click in React?React