WordPress Plugin Hooks: Plugin Finished Loading

Now that I have a plugin that can be activated and deactivated, it might be nice to know when the plugin has completed loading.

In the code below, I have added a function for this (line 43) , and an action hook (line 48). I’ve also added some comments to help explain things better. Also notice the echo statement (line 44). It’s only there for development of course, but it proves that the function is being called.

It is interesting to note that this action fires after *all* activated functions are finished loading. This could come in handy if you need to reference a different plugin from this one.

API reference: http://codex.wordpress.org/Plugin_API/Action_Reference/plugins_loaded

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.

 

Conclusion

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