Need a WordPress website this weekend? Start here...

Register Activation Hook – Three necessary and sufficient conditions

(Reading time: 1 – 2 minutes)

This is the last blog post in a series on the register_activation_hook function in WordPress.

I promised I would clear everything up in this last article. So here’s clarity, three conditions necessary and sufficient for using register_activation_hook function:

  1. The function call has to be in a file that will be scanned by the WordPress plugin scanner.
  2. The function call must have as it’s first argument the filename containing the plugin name.
  3. The function call must have as it’s second argument a callback that’s in scope.

It’s really, really simple, but I’ve never seen these three conditions listed out anywhere on the web.

So there they are.

register_activation_hook series

Here’s the other articles in this series:

WordPress Plugin Activation using PHP classes

(Reading time: 2 – 3 minutes)

In the last two DIY WordPress articles we examined the basic structure of registering an activation hook for a plugin, and we found out what, exactly, comprises a minimal WordPress plugin.

Next, we’re going to step it up, and show one way plugins built using PHP classes register activation functions.

We’re going to do it with two files, a driver file and a class file. Let’s take a closer look.

Plugin driver file

The driver file:

  1. loads the class file;
  2. instantiates the class;
  3. registers the functions for activation and deactivation (hooks).

Here’s what the code looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
/*
 * Plugin name: A Class Activator
 */
include dirname(__FILE__).'/class.php';
$myclass = new MyClass();
 
/* Make the class works first */
if (isset($myclass)) {
   register_activation_hook(__FILE__, array($myclass,'activate'));
   register_deactivation_hook(__FILE__, array($myclass, 'deactivate'));
}
?>

Let’s take a look at the class next.

Plugin class file

For this example, the class is simple, just the activate and deactivate function.

1
2
3
4
5
6
7
8
9
10
11
12
<?php
Class MyClass {
 
   function activate() {
      add_option('a_myclass','Activated');
   }
 
   function deactivate() {
      delete_option('a_myclass');
   }
}
?>

These functions can be named anything you want, say, “foo” and “bar.”

Simple, right?

This isn’t the only viable pattern using classes. There’s many more, experiment a bit, find your own pattern that works.

More plugin activation coming

And stay tuned, because next week concludes this series with a short article giving you three necessary and sufficient conditions for register_activation_hook. It’s easier than you think, and you’ll see why we needed to take a look at how WordPress deals with named plugins last week. Once you understand these three conditions, you won’t have to experiment, you can just design.

As it should be.

If you have any questions, please leave a comment. Alternate techniques? Leave a comment as well. I’ll clean up the formatting if necessary, or just add it in here with a link to your website.

Finally, I’ll give the first person who can guess why I named the plugin “A Class Activator” and the option “a_myclass” an hour of blog post engineering.