WordPress plugin Hooks: Activation and Deactivation

Ok, so now I’ve got the beginnings of a plugin.

Time to learn about hooks!

However, first, notice that I’ve added text related to the actual license. Turns out that’s a proper practice, so I’ve added it as follows:

Ok, so now what I want to do is make the plugin do something.

The first thing that the user is going to do is activate the plugin. So I need a “hook” for that:

What I’ve done here is add a function at line 28 that will be called when the user clicks the activate button.

The code on line 32 is the hook. It tells WordPress to call the function at line 28 when the activate button is pressed.

Of course, if the user wants to remove the plugin, I’ll need to provide a way to undo any setup that might be done in the activation handler. That is accomplished with another function and hook:


On line 32, there is a function which will run upon deactivation. The hook is on line 37.



At this point, I have a basic starting point for a plugin. The plugin should activate and deactivate successfully, and if there was code in either function, it would be executed at the appropriate time.


API Reference: http://codex.wordpress.org/Function_Reference/register_activation_hookhttp://codex.wordpress.org/Function_Reference/register_deactivation_hook

WordPress plugins: Where to begin

Here is one place you can start if you are looking for basic info on creating a plugin:



Create a file in the plugins folder. Call it whatever you’d like.


Place a comment block at the top like this, filling in the info specific to your plugin:

At this point, you will see your Plugin in the plugins section of the site admin.