Thursday, February 17, 2011

How to use WordPress Action Hooks

Why Action Hooks?
Let's think there is no concept called Action Hooks. When we wanted to change or extend some function of WordPress then probably we wanted to change core files of wordpress installation. And when it comes time to upgrade wordpress, you'll have to recoding all the modifications you have done to prior version files, or you'll have to stick with the old version.


What are Action Hooks.
When it comes to above type of scenario wordpress action hooks are become more handy. For an example let's take very common wordpress action like "wp_head" or "wp_footer". Using action hooks, we can execute a function when certain wordpress action(action we define in hook) execute.

For an example, I need to perform my function "addNewCss" when wordpress action wp_head encounter. To do that simply I can use wordpress hooks

<?php
function addNewCss() {
//function content goes here.
}
add_action('wp_head', 'addNewCss', 5);
?>

This will execute my function addNewCss when it encountered wp_head action and the priority is 5. Which makes run this function earlier. Default is 10. (setting it to 5 would make it run earlier and setting it to 12 would make it run later.


Creating Own Action Hooks
Sometimes you may want to create own action hooks then others can extend your code functionality without touching your codes and make those in separate files. To do that use,

<?php do_action('mynew_action_hook'); ?>

Now, anyone can come hook their own function to your "mynew_action_hook".

13 comments:

VPS Hosting said...

WordPress is often best when you use the wp_enqueue_style () Additional elements of the head style. I think it's important to say this, for example to add CSS hook wp_head head.

kids worksheets said...

This article is a (hopefully complete) list of the action hooks available for use in plugin development. Wordpress plugin allows easy modification and customization.

thesis writing service said...

it is desirable to present the material more concise and less ornate. And thank you in other matters. How long you have been looking for this one

emt training said...

Thanks for sharing the code, it was really a helpful code for programmers like me.

pharmacy technician said...

I want to share this urgent program to my students.Impressive!

grapevine botox said...

Thank for this, at least I can slowly learn about this. This could also help others. This information must be spread.

texas graphic designers said...

I was able to read the whole content, all I can say is this is great! Great post with great ideas with great thoughts with great concept and with such great writer. That was perfectly written and very much easy to comprehend.

custom poker chips said...

So embarrassing article you've made it. I actually finding some information about action hooks.

Electrician Schools in Texas said...

Thanks for posting this one to us.

martinez vs chavez said...

If I'm sure the costumers will purchased it for a great advertisements you have.

LA Party Bus rentals said...

I agree about the tips and information imparted.

retro tee shirts said...

The awesome new posts keep me coming back here time and time again. thanks again so much.

is shingles contagious said...

Excellent post, though I’m still trying to get my head round this!
I want to insert 2 x fixed width divs inside #header of Thematic 0.9.5, moving all your header content into the left div and creating some custom content in the right div. Not sure whether I should be unhooking or creating a new action hook from scratch and somehow referencing some of your content in header-extensions.php?!?! Could you possibly offer any advice or instructions?
Any help would be appreciated.

Post a Comment