Pippins Plugins
  • Email
  • Facebook
  • Feedburner
  • Github
  • Google
  • Twitter
  • Vimeo
  • Youtube
  • Rss
  • About
  • News
  • Join the Site
    • Member Benefits
    • Member Plugins
    • Email Notifications
  • Plugin Store
    • Affiliate Area
    • Checkout
  • Plugins
    • Plugin Portfolio
      • Plugin Portfolio – List View
    • Free
    • Premium
    • Member Plugins
    • Coding Standards
    • Get Plugin Support
  • Tutorials
    • Series
      • Plugin Development 101
      • Creating a User Follow System Plugin
      • Customizing Restrict Content Pro
      • Displaying Content with Easy Content Types
      • Writing Your First WordPress Plugins, Basic to Advanced
      • Working with Widgets
      • User Submitted Image Galleries
      • Plugin Thoughts
      • Integrating Stripe.com with WordPress
      • WordPress Rewrite API
    • Member Exclusive
      • Free Members
      • Subscriber Only
    • Difficulty
      • Beginner
      • Intermediate
      • Advanced
    • Action and Filter Hooks
    • Ajax
    • Custom Post Types
    • External APIs
    • Short Codes
    • Taxonomies
    • Video Tutorials
    • Widget Tutorials
    • WordPress Admin / Dashboard
    • Working with jQuery
    • WordPress Database
    • Writing Plugins
    • Tag Index
  • Reviews
  • Support Forum
  • Contact
    • Support the Site
    • Request Code Review
    • Plugin Support

Quick Introduction to Action Hooks

Posted on January 26, 2012 by Pippin in Action and Filter Hooks, Intermediate, Tutorials, Writing Plugins 2 Comments
Home» Tutorials » Action and Filter Hooks » Quick Introduction to Action Hooks
Fishing Hooks
Tweet
Love It - 0

Action hooks are used in WordPress to perform functions, or “actions”. They are used throughout many plugins and themes to provide an easy way for users of the theme or plugin to modify the output or the way it functions. This quick tutorial will give you a good introduction into how action hooks work.

So what exactly is an action hook? Basically it is a place in a plugin or theme that other functions can be “hooked” to, meaning that a function can be written that outputs some content and then be attached to the action hook; the location of the action hook determines where the custom function displays its information.

Action Hooks are not only used for displaying information, they can be used for much more than that, but for the purposes of this tutorial, I’m only going to show you how they work in regard to displaying (or not displaying) information.

There are two integral parts to action hooks:

  1. do_action() – this is where the “hooked” function is run
  2. add_action() – this attaches a function to a hook, which is defined by do_action()

A couple of other functions come into play when working with hooks, but I’m not going to worry about covering them here. These are the only two you need to get started.

So let’s assume for a moment that we have setup a plugin that displays information on the front end of our WordPress. For simplicity’s sake, we are also going to assume that we have manually added our function to one of your theme’s templates. The sample function below is going to represent our main plugin function that displays information:

1
2
3
function pippin_sample_output_function() {
	echo '<div id="extra-content">This is content added to the WordPress footer.</div>';
}

This is nothing more than a simple echo function. It just outputs a sentence wrapped in a DIV tag.

Now we’re going to add a hook to our sample function. This hook is going to allow other functions, such as ones from other plugins, to add more content to this sample output function.

1
2
3
4
function pippin_sample_output_function() {
	do_action('pippin_before_output');
	echo '<div id="content">This is just sample content</div>';
}

The only thing I’ve added is the do_action() hook. This new custom hook will allow us to tie into the function from other functions to add more custom content. Here’s an example:

1
2
3
4
function pippin_sample_hooked_function() {
	echo '<div id="before-content">This is outputted by the hook, before the original content.</div>';
}
add_action('pippin_before_output', 'pippin_sample_hooked_function');

This will resulting output that looks like this:

1
2
<div id="before-content">This is outputted by the hook, before the original content.</div>
<div id="content">This is just sample content</div>

Let’s go back and modify our original function again to add another hook:

1
2
3
4
5
function pippin_sample_output_function() {
	do_action('pippin_before_output');
	echo '<div id="content">This is just sample content</div>';
	do_action('pippin_after_output');
}

So we now have two hooks, one for before and one for after our normal content. This means we can now add another hooked function:

1
2
3
4
function pippin_another_sample_hooked_function() {
	echo '<div id="after-content">This is outputted by the hook, after the original content.</div>';
}
add_action('pippin_after_output', 'pippin_another_sample_hooked_function');

Adding this second hooked function will result in this:

1
2
3
<div id="before-content">This is outputted by the hook, before the original content.</div>
<div id="content">This is just sample content</div>
<div id="after-content">This is outputted by the hook, after the original content.</div>

Why have I added two hooks, one before and one after? Well, it’s quite simple really: I want to provide my users as much flexibility as possible. When building a plugin or theme, hooks like these can provide a huge advantage to users who wish to modify the plugin or theme. By putting a hook before and after, I’ve just given my users the ability to add content where they want (either before or after), and they do not have to modify the core plugin or theme files to do so.

It is quite common for good plugins / themes (at least the larger ones) to have upwards of 15 to 30 hooks that function similarly to the examples above. I have several real examples for you:

  • Swagger – The theme framework by Jason Bobich (also the theme this site is running) has dozens and dozens of hooks. And this is great! As a user, I was really easily able to add some extra content to my header section without having to modify any of the theme files.
  • JigoShop – The popular e-commerce plugin that has dozens of hooks. I setup an e-commerce store using it once and was able to completely modify the output of products because of the large number of hooks the developers included.
  • Restrict Content Pro – My premium content / subscription manager plugin has somewhere in the range of 15 – 20 hooks that can be used to add extra registration fields, or content before or after any of the membership forms.

Related Items
  • Plugin Development 101 – Intro to Actions
Tweet Follow @pippinsplugins
add_action, do_action, hook

2 comments on “Quick Introduction to Action Hooks”

  1. Gordon McNevin says:
    March 16, 2012 at 8:11 am

    Thanks for the very easy to use tutorial, hooks aren’t that hard after all :)

    P.S. I also use Swagger on one of my sites, it rocks!

    Reply
    • Pippin says:
      March 16, 2012 at 9:11 am

      Nope, hooks are pretty simply once you get your mind around them.

      Swagger is a great theme :D

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Login

Lost your password?

Please enter your username or e-mail address. You will receive a new password via e-mail.

  • Facebook Become a Fan Like

  • Twitter Subscribe on Twitter Follow

  • YouTube Follow my Videos Subscribe

  • RSS Feed Subscribe with RSS Subscribe

Easy Digital Downloads

Most Loved

  • Love It Pro for WordPress
  • Write a “Love It” Plugin with Ajax to Let Users Love Their Favorite Posts / Pages
  • Simple Notices Pro Plugin for WordPress
  • User Bookmarks for WordPress
  • Front End Registration and Login Forms Plugin

Similar Plugins and Posts

  • Plugin Development 101 – Intro to Actions
  • Using pre_get_posts to Modify Queries for Meta Data and More
  • Let’s Talk Extensible Code
  • Create a Live Search in WordPress with jQuery and Ajax
  • Adding Custom Fields to the Easy Digital Downloads Checkout

Latest Premium Content

  • Plugin Development 101 – Introduction to Adding Dashboard Menus
  • Plugin Development 101 – Intro to Loading Scripts and Styles
  • User Follow System – Part 5
  • Plugin Development 101 – Intro to Short Codes
  • Plugin Development 101 – Registering a Custom Post Type
  • Plugin Development 101 – Intro to Actions

Latest Tutorials

  • Test Your Plugins with RTL (0)

    Right-To-Left languages are those that...

  • Submitting Your First Pull Request to a WordPress Plugin on Github (5)

    Github is an extremely popular tool for managing WordPress plugins, and one...

  • Plugin Development 101 – Introduction to Adding Dashboard Menus (1)

    Adding new menus, both top level and sub level, to the WordPress Dashboard is a really common task for plugins...

Enter your email to receive automated updates when new posts are published

Latest Tweets

  • RT @toscho: #WordPress: How post meta fields work. http://t.co/uDRaDu0EsS
    May 25, 2013
  • RT @strickland: Afternoon crowd: To celebrate Memorial Day weekend @gittyapp is on sale through Monday. Now is the time to join in! http:/…
    May 25, 2013
  • .@itsananderson wins!
    May 25, 2013

Topics

attachments add_shortcode campaign monitor meta box Rémi Corson Tom McFarlin the_content wp_enqueue_script add_options_page shortcodes Sugar Event Calendar featured contextual help mail chimp login authors attachment Related posts image plugin forms do_action short codes apply_filters post types bbpress recent posts comments short code taxonomies custom post type images gallery Ajax Stripe taxonomy jquery widgets users add_filter easy content types add_action widget restrict content pro easy digital downloads

Weekly Newsletter

Useful Links

  • Join the Site
  • Plugin Store
  • Affiliate Area
  • Tag Index
  • Support the Site
  • Suggest a Tutorial
  • Random Post
  • Contact

Monthly Archives

(c) 2013 Pippin's Plugins