888888.888888.88""Yb..dP"Yb..8888b..Yb..dP.88b.88....db....8b....d8.88..dP""b8..dP"Y8
88__...88__...88__dP.dP...Yb..8I..Yb.YbdP..88Yb88...dPYb...88b..d88.88.dP...`".`Ybo."
88""...88""...88"Yb..Yb...dP..8I..dY..8P...88.Y88..dP__Yb..88YbdP88.88.Yb......o.`Y8b
88.....888888.88..Yb..YbodP..8888Y"..dP....88..Y8.dP""""Yb.88.YY.88.88..YboodP.8bodP'


88b.88.888888.888888.Yb........dP.dP"Yb..88""Yb.88..dP
88Yb88.88__.....88....Yb..db..dP.dP...Yb.88__dP.88odP.
88.Y88.88"".....88.....YbdPYbdP..Yb...dP.88"Yb..88"Yb.
88..Y8.888888...88......YP..YP....YbodP..88..Yb.88..Yb

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 Chat
Wordpress plugins chat?
3 comments
page 1308
Post Image The Easy Peasy Way
Calling for images using php wordpress?
26 comments
page 1065
3 Ways To Speed Up Your Blog Without A Cache Plugin
Comment on a few ways to speed up your blog?
one comment
page 1321
Updating Code Snippets Here
Code snippets?
one comment
page 1338
Wpunlimited The Ultimate Wordpress Theme
Html5 wordpress?
3 comments
page 1141
Fun With Theme Widgets
Funy photo widget?
24 comments
page 867
Quick N Dirty Admin Login Screen
Login page css templates?
no comment
page 128
Wpunlimited The Ultimate Wordpress Theme
Html5 wordpress?
3 comments
page 1141
Post Image The Easy Peasy Way
Wordpress attach image to multiple posts?
26 comments
page 1065
Using Your Own Url Shortener
Wordpress clear rewrite rules?
4 comments
page 1190
Quick N Dirty Category Redirection
Wordpress redirect category?
no comment
page 133
Html 5 Gallery
Themeatic html5?
6 comments
page 1305
Premium Ithemes Review Photo Gallery
Ithemes review?
4 comments
page 226
Updating Code Snippets Here
Wordpress fun plugins?
one comment
page 1338
Updating Code Snippets Here
Wordpress fun?
one comment
page 1338
Theming Habari Vs Wordpress
How to design a habari theme?
13 comments
page 440
Why I Ditched Disqus
Disqus email?
5 comments
page 1175
Post Image The Easy Peasy Way
Wordpress first image?
26 comments
page 1065
Quick N Dirty Admin Login Screen
login screen using css?
no comment
page 128
Html 5 Gallery
Picture gallery html5?
6 comments
page 1305
Post Image The Easy Peasy Way
Resizing images in thecontent?
26 comments
page 1065
Post Image The Easy Peasy Way
Wordpress first image gallery?
26 comments
page 1065
Html 5 Gallery
Html 5 photo gallery?
6 comments
page 1305
Post Image The Easy Peasy Way
Inserting an image url in comments?
26 comments
page 1065
Post Image The Easy Peasy Way
Wp attachment functions?
26 comments
page 1065
Dont Mess With My Toot Toot
Wordpress custom content?
16 comments
page 599
Using Your Own Url Shortener
String shortner?
4 comments
page 1190
Quick N Dirty Admin Login Screen
My admin login page?
no comment
page 128
Using Your Own Url Shortener
How to build a shortner link?
4 comments
page 1190
Theming Habari Vs Wordpress
Habari vs wordpress?
13 comments
page 440
Post Image The Easy Peasy Way
Get post attacments?
26 comments
page 1065
Are Child Themes The Best Option
Wp child themes?
15 comments
page 1262
Upload From Url
Upload to url?
6 comments
page 326
  1 query every 1232 seconds, updated 1 seconds ago.
Thursday, 2am
 __
(__)
   `

 Austin

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

Wednesday, 8am
 __
(__)
   `

 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.

Wednesday, 8am
 __
(__)
   `

 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.