Using WordPress Plugin Activation and Deactivation Hooks

Using the activation and deactivation hooks in a WordPress plugin is simple process. These hooks are often necessary to develop most plugins.

A WP hook allows you to execute your own code in a particular situation. In this case, we want to execute code when our plugin is activated or deactivated.

WordPress Activation Hook

When someone installs your plugin on a new site, there are often default settings or database tables that need to be created so the software can work correctly.

The activation hook is called when a new plugin is activated on a site, which allows you to execute code upon activation.

To accomplish this in your coding, first you need to register the activation hook. When you do this, you also specify the name of a function that should be called, which is what holds the actual activation code that you want to run.

Always be sure to use a prefix with your functions to ensure another function with that same name won’t exist elsewhere. Personally, I usually use the plugin name or some abbreviation of that name for the function prefix.

The example below registers the hook to call the function named rsplugins_activation. Obviously, you should change this function name to something unique for your own product.

__FILE__ is a PHP constant that provides the full path of the current file name, which is required as the first parameter of this hook.

Please keep in mind that all code examples shown on this page are examples of PHP coding.

register_activation_hook(__FILE__, 'rsplugins_activation');

WordPress Deactivation Hook

When your plugin is deactivated on a site, WordPress will call the deactivation hook to see if you have code that needs to run.

In most situations, the deactivation code will clean up anything that the activation code placed on the site (settings, database tables, etc). However, you may want to consider if this is the right thing to do for each product you develop.

In some situations, you may actually want to make a separate uninstall function that can be run from the plugin administration, while the actual deactivation hook does not remove all of the settings. This prevents users from accidentally wiping out work they’ve done with your software.

The example below registers the deactivation hook to call the function named rsplugins_deactivation. Once again, make sure you change the function name from this tutorial to the name of a function that you create for this purpose.

register_deactivation_hook(__FILE__, 'rsplugins_deactivation');

Plugin Activation & Deactivation Functions

Once you have the proper code in your plugin to call the activate and deactivate hooks, the last step is to add functions that are called by those hooks.

For this tutorial, we will use different functions for the activation and deactivation hooks.

Some example code has been provided below to show the basic structure of these two functions using the names specified in the register hook codes above.

function rsplugins_activation()
// Plugin Activation Code Goes Here
// Any code placed here will run when the plugin is activated in the WP dashboard
// This is a great place for database table creation or license validation to occur

function rsplugins_deactivation()
// Plugin Deactivation Code Goes Here
// Code here will run when the plugin is deactivated when a user intends to stop using it
// Database tables and custom WP options can be removed here to clean up any trace of the installation

You will need to add your own code inside of these functions. Consider what needs to be done to a website or database when your plugin is installed for the first time and then when it is uninstalled.

As an example, if want to create a simple data entry plugin then you will need to create a database table to be able to save that data. The activation function is the perfect place to include PHP code to create this database table with the appropriate fields for the information you want to save. The deactivation function can then be used to remove that database table, if desired.

Share the Post:

Related Posts