December 3, 2008

When you are writing a plugin, does it ever bother you that there are occasions when you need to add a hook, to call a function, to call another function? Menus are a good example. In some other blogging systems it isn’t necessary to manually add the hook itself, and you can emulate that quite easily in WordPress.

Here’s a quick example that shows a way of automatically creating hooks, and also gets that functionality out of the way so you are free to concentrate on your plugin.

First create a class. I have called it plugin, but you will need to choose something unique to make sure it doesn’t conflict. Use this code:

[php]
class plugin{

public function __construct(){

$methods = get_class_methods($this);

foreach($methods as $method){
if ( is_int( strpos($method , 'filter') ) ){
$filter_name = str_replace('filter_' , '' , $method);
add_filter( $filter_name , array($this,$method) );
} elseif ( is_int( strpos($method , 'action') ) ){
$filter_name = str_replace('action_' , '' , $method);
add_action( $filter_name , array($this,$method) );
}
}

if( method_exists($this,'init') ){
$this->init();
}

}

}

What this does is quite simple. When the class is instantiated it gets a list of the methods and checks each one for the prefixes, filter, and action. If it finds a function with one of those names it chops the prefix off and registers the rest as a hook.

For example: A method with the name filter_the_content, would be registered as a filter using the_content hook.

Finally, it checks to see if the class has a method called init and if it does it runs it.

To use this you need to extend it. The following example shows how to filter the content:

[php]
class ed_balls extends plugin {

function filter_the_content($content){

return "Ed Balls";
}
}

This replaces your content with the words, “Ed Balls”. Not useful in itself but it serves as an example.

If you still wanted to use some hooks manually, perhaps you want to set the priority, or change the number of arguments passed you can use the init function and treat it as thought it were the constructor.

For example:

[php]
class ed_balls extends plugin {

function init(){

add_action('wp_footer',array($this,'add_footer'),1);

}

function add_footer(){

echo "a footer";

}
}



Wordpress title showing space?
no comment on page 1371

Wordpress fun?
one comment on page 1376

Live blogging plugin?
4 comments on page 1258

Wordpress 3 admin speed up?
4 comments on page 1321

Framework photoshop?
3 comments on page 296

Fun wp plugins?
one comment on page 1376

Habari vs wordpress?
12 comments on page 440

Wp tags vs categories?
12 comments on page 7

Wordpress rss seo?
one comment on page 1361

Photo albums html5?
6 comments on page 1305

Wordpress chat?
no comment on page 1308

Wordpress exif data?
12 comments on page 230

Css sidear tab?
2 comments on page 336

Wordpress theme html5 blueprint?
6 comments on page 1305

Wordpress shortcode in plugin?
no comment on page 236

Html 50 photo album?
6 comments on page 1305

Get the post attachement?
24 comments on page 1065

Wordpress plugin development 30?
one comment on page 1373

Wordpress plugin development 30?
one comment on page 1373

Disqus formatting?
7 comments on page 1175

Html5 photoalbum?
6 comments on page 1305

Html5 photoalbum?
6 comments on page 1305

Wordpress fun?
one comment on page 1376

Fun wordpress plugins?
one comment on page 1376

Url shortener ideas?
4 comments on page 1190

Url shortener ideas?
4 comments on page 1190

Html 5 photo gallery?
6 comments on page 1305

Multiple post navigation?
no comment on page 1147

Html5 photo galleries?
6 comments on page 1305

Adding images to a wordpress 3 post?
24 comments on page 1065

Html5 photo gallery code?
6 comments on page 1305

Wordpress multiple blog master?
one comment on page 1376

Wordpress 3 tableprefix?
one comment on page 1376

Wordpress 3 tableprefix?
2 comments on page 1374

Using wordpress as a framework?
2 comments on page 335

Single post image size?
24 comments on page 1065

Get featured image src wordpress?
24 comments on page 1065

Disqus wordpress mu?
7 comments on page 1175

Image gallery html 5?
6 comments on page 1305

Wordpress theimage?
24 comments on page 1065

Wpgetattachmentimagesrc size?
24 comments on page 1065
  every 1730s, 1s ago, in 0.03s.
 __
(__)
   `
 Austin

I really like the idea. You should consider making “plugin” an abstract class.

 __
(__)
   `
 Andrew Rickmann

That's a good point wesley. The other option that I considered was to do as Habari does and have a specific function to add all the priorities into an array so the constructor can handle them. I wanted to keep it very simple though. I might expand the post tomorrow to include that.

 __
(__)
   `
 wesley

Very nice, only issue is, for the custom priority etc, your function can no longer have the name action_add_footer, which is inconsistent with the rest of the function names.

Perhaps better would be to have an array of these special functions that should be ignored by the constructor.


0s