Using The 3rd Parameter With Action Hooks – Priority For Positioning

Once you’ve got your head around basic custom functions, you’ll probably run into code that uses the same hook and need to add a third parameter to your own custom functions.

Lets take a look at a very simple example:

  1. The first parameter is the hook – genesis_after_header
  2. The second parameter is the custom function – after_header_content

As you can see, this code above hooks into the genesis_after_header hook and displays the text between the div tags.

The only problem is, by default in some themes, Genesis displays the primary nav menu in exactly the same position or hook location.

So basically speaking, the code executes and displays the content after the hook location as there’s another function using the same hook.

How do we fix this?

We simple add a third parameter to the code to include a priority number.

Considering the fact the default priority is 10, you won’t need to add a number which is higher than that if we want to display the content after the primary nav menu. The reason for this is the functions in the parent theme execute first and any other functions using the same hook execute after meaning they display after the default hook location.

Or

You’ll need to add a priority number as the 3rd parameter which is lower than 10 if you want the content to display before the primary nav menu using the same hook, genesis_after_header.

Let’s go over the 3 parameters again:

genesis_after_header', 'before_primary_nav_menu', 8
  1. The first parameter is the hook – genesis_after_header
  2. The second parameter is the custom function – before_primary_nav_menu
  3. The third parameter is the priority number – 8

So you can see from these 3 examples which all execute using the genesis_after_header hook that adding a 3rd priority determines when the custom function executes resulting in which position the content displays. Before or after the default hook location.

Position In Functions File

What if you have more than one custom function in your child themes functions all using the same hook?

If your functions don’t contain a third parameter for the priority, the execution position on the same hook is determined by the position the custom functions are placed in your functions file.

So the higher they are in the file determines the position they execute on the same hook.

Clearly this is overridden by adding a third parameter to the action and any default functions in the parent themes functions.php file.


Comments

6 responses to “Using The 3rd Parameter With Action Hooks – Priority For Positioning”

  1. Thanks for the helpful post.

    You helped me create a function sometime last year and gave it a priority of 5. The priority for the one in this post is 8. For the new function I’m working with, either priority works, but I’m wondering why there’s a range of numbers.

    Is for multiple functions within one hook? So if the default priority is being used by the last function that displays beneath my posts, I can use any number that’s less than 10 to place the first function above it?

    It works, of course, when I test it. But I’m just wondering if there’s any reason I should avoid a priority of 1. It’s also curious that zero works just as well and doesn’t remove the function, as I thought it would.

    :o)

    1. Brad Dalton Avatar
      Brad Dalton

      Carla

      Support information. http://wpsites.net/registration/

  2. Hi Brad.

    I hate the generic ‘thanks’ comments, but the addition of priority to hooks has been one area that I’ve taken a while to get me head around. Thanks for the really clear post. The simple mention that 10 is the default priority made all the difference.

    1. Brad Dalton Avatar
      Brad Dalton

      Hi Alistair

      There’s a chart floating around somewhere which compares the old and new hooks and also includes the 3rd parameter in each hook.

      Once i find it, i’ll publish it here on wo sites.

      You sound like you’ve worked out that this is needed to unhook, hook in and filter new and existing content in different theme positions.

  3. Hello Brad,
    I dont usually respond in comments like this, but I have been looking for ages to try and get an adsense ad below the title/date, but BEFORE the text starts so that the text wraps around a floated left div ad.
    Used this code example, changed the priority to 5 (I knew I needed to change the priority but hadnt seen anyone do it in code). This code helped tremendously. Thanks Brad!

    1. Brad Dalton Avatar
      Brad Dalton

      Anytime Bruce.

Leave a Reply

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.