Using WordPress Plugin Activation and Deactivation Hooks

Although the concept behind using the activation and deactivation hooks in a WordPress plugin is fairly simple, these hooks are necessary to make almost any plugin function properly.

A WP hook allows you to execute your own code in a particular situation.

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.

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
}

function rsplugins_deactivation()
{
//Plugin Deactivation Code Goes Here
}

The actual code that goes in these functions can widely vary from one plugin to the next. Other tutorials on this site will likely be useful to you if you are not sure about what code to use when you activate or deactivate a plugin. Take a look at training lessons on Working with WordPress Options and WordPress Database Use to get started.

Comments are closed.