You are here: Home » Extending WordPress » WordPress Plugin Activation using PHP classes

WordPress Plugin Activation using PHP classes

by Dave Doolin on March 16, 2010 · 10 comments

(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.




Would you like more? Send me a letter...
"Hi Dave,
Website In A Weekend seems pretty cool. I'm serious about this WordPress and web stuff, and I'd like to keep up with it. My name is and my email address is . I'm comfortable with email newsletters. I know you will protect my privacy, and that I can unsubscribe at any time. "

{ 10 comments }

Heather March 16, 2010 at 2:28 am

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 My ComLuv Profile

Dave Doolin March 16, 2010 at 7:49 am

Debugging and testing tools for web coding are way behind desktop coding. That’s what makes it hardest for me.
Dave Doolin´s last blog ..DIY WordPress – World’s Shortest Plugin My ComLuv Profile

Heather March 16, 2010 at 8:06 am

It’s the syntax that throws me off; there’s no logical reason for this since it’s actually simpler than some of the other languages I’ve coded in, but it just messes me up completely.
Heather´s last blog ..A Question of Manga; Whose side are you on? My ComLuv Profile

Dave Doolin March 16, 2010 at 8:37 am

Yeah, I have the same issue.

Time in the saddle, with php.net open in the browser solves all.

Heather March 16, 2010 at 8:42 am

Time to tinker would be nice, I’ll have to see what I can carve out of other things (probably not for a while though).
Heather´s last blog ..A Question of Manga; Whose side are you on? My ComLuv Profile

Deacon March 16, 2010 at 7:38 am

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 My ComLuv Profile

Dave Doolin March 16, 2010 at 7:47 am

hehe yep, $hisclass, $herclass, $lowclass, $noclass, you got it.

PHP is really procedural though. OO was bolted on afterwards.
Dave Doolin´s last blog ..A Simple Guide To Wordpress Theme Installation My ComLuv Profile

Deacon March 16, 2010 at 11:08 am

I think I see what is going on here. There are a couple Wordpress-defined functions in here that I don’t quite know the context for.

I think this goes on the “some day” list.
Deacon´s last blog ..Fighting Entropy, Our Environment’s Effect on Productivity My ComLuv Profile

Chris Roane March 16, 2010 at 7:54 am

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 My ComLuv Profile

Dave Doolin March 16, 2010 at 8:32 am

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 My ComLuv Profile

Comments on this entry are closed.

Previous post:

Next post: