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

Check for Existence of Action Hook

Posted on February 15, 2012 by Pippin in Action and Filter Hooks, Intermediate, Quick Tips, Tutorials 2 Comments
Home» Tutorials » Action and Filter Hooks » Check for Existence of Action Hook
Fishing Hooks
Tweet
Love It - 0

Action hooks allow us to make our plugins much more extensible, and some times we need to be able to check whether a hook exists, or has been registered. We might need to check that a hook exists for a variety of reason, and thankfully WordPress gives us a really easy way to do that. Using the has_action() function, we can run functions conditionally, based on whether our specified hook exists.

The has_action() conditional works just like the has_filter(): we simply pass the name of the action we want to check for. Like this:

1
2
3
4
5
6
if(has_action('name_of_action_to_check_for')) {
	// action exists so execute it
	do_action('name_of_action_to_check_for');
} else {
	// action has not been registered
}

This has a lot of real world applications. I just used it today in my Easy Content Types to output some special field-type-specific information. I needed to check that the action hook for each individual field type was registered, output the information from the hook if it was, and then display default information for any field type without an action registered. The way that I used it was like this:

1
2
3
4
5
6
7
 
<?php 
if(has_action('ecpt_field_options_' . $field->type)) {
	do_action('ecpt_field_options_' . $field->type, $field); 
} else { ?>
	<p class="description">No options for this field</p>
<?php } ?>

This conditional runs for every meta field type registered in the plugin. $field->type refers to the type of field, so for my “textarea” field, the hook is named ecpt_field_options_textarea. By doing it this way, I can very easily specify custom HTML for each field type (including those registered through add-on plugins), while still displaying default information for fields without a hook registered.

Pretty cool right?

This is actually very similar to the way the wp_ajax_ hook works, which you will see if you go explore the WordPress core admin-ajax.php file.

Tweet Follow @pippinsplugins
add_action, do_action, has_action

2 comments on “Check for Existence of Action Hook”

  1. chuck scott says:
    April 6, 2013 at 2:38 pm

    Hey Pippin – just wanted to leave appreciative comment and say, “THANKS” for this post … i was knocking my head around with “if function_exists” when trying to determine if an action was present and was only after googling around a bit and came across your excellent post on using if has_action (and/or has_filter) which helped crack the code (sorry, pun slightly intended) … accordingly, best of ongoing success with all things WP et al. :>) cheers – chuck scott

    Reply
    • Pippin says:
      April 6, 2013 at 2:47 pm

      Great to hear!

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

shortcodes hook add_options_page contextual help add_shortcode Tom McFarlin get_user_meta meta box campaign monitor featured Rémi Corson wp_enqueue_script Sugar Event Calendar attachment image do_action forms plugin mail chimp login short codes authors Related posts bbpress comments recent posts post types apply_filters 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