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

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.

Comments

  1. Heather says:

    Not even going to try and guess; mostly because at the sight of code my mind’s gone off into C++ mode and actually wants to play with code. Cruel person.

    Interesting though, never really got into webcoding beyond the basics but it doesn’t look so bad here.
    .-= Heather´s last blog ..Webcomic: The Mayans Day 2 =-.

  2. Deacon says:

    It’s been about 8 years since I’ve done any “serious” coding, so please bear with me.

    I remember you mentioning that PHP is an object oriented program, so when this code runs, is $myclass now an object of type MyClass() in your plugin? If so, could you create another MyClass() object, called, say, $yourclass?
    .-= Deacon´s last blog ..Fighting Entropy, Our Environment’s Effect on Productivity =-.

  3. Chris Roane says:

    This is great! I’ve been wanting to get into wordpress plugin development for a long time (I am a PHP programmer).

    This will definitely keep me coming back. Thanks for posting it.

    One question that I have, and this may not be the correct place to post it. But does creating wordpress plugin bring a lot of traffic to a website (even if it doesn’t become super popular)?
    .-= Chris Roane´s last blog ..New Logo, Site Updates and the Future of MT Programmer =-.

    • Dave Doolin says:

      Chris, if you write a useful plugin that people really like, you stand a decent chance of getting some pretty high quality links from people using it. You might possibly get linked from WordPress.org as a featured plugin if it’s really good. That doesn’t hurt your page rank or Alexa ranking.

      I could limit my posting to just the hRecipe plugin page and retain 30+ hits per day on just that. More if I released more often. That doesn’t sound like much… but it would be very highly targeted traffic… more so than the blog.

      Poke around here for more information. I should write a little tutorial to accompany the demo code I have. Probably will at some point.
      .-= Dave Doolin´s last blog ..DIY WordPress – World’s Shortest Plugin =-.