Using Hook Priorities In Genesis

Hooks are amazing. They allow adding/removing/changing functionality without editing any core files. In a modern framework such as Genesis, hooks form the basis of nearly all customization. For example, to add some text just after the site header, you would do this:

add_action('genesis_after_header', 'some_function');

Where `some_function` prints something. But what happens when this hook trivia becomes bigger than little tweaks? What if you have to add/remove multiple things, or override actions defined in the framework. Someone recently asked on Google+

How do I add the secondary navigation above the footer in a Genesis child theme?

Well that’s simple, just hook the navigation to the appropriate hook(*genesis_before_footer*) in this case. But let’s see what happens in one particular case.

Screenshot from 2014-02-28 19:03:39

There’s our problem, the navbar displays below the footer widgets. This can be traced back to framework code. On line 14 of *lib/structure/footer.php*, you’ll find this

add_action( 'genesis_before_footer', 'genesis_footer_widget_areas' );

Now we’re assigning our function to the same hook, but it runs after the one defined in the framework. Why does this happen, and how can we correct this?

Enter Hook priorities

The WordPress codex entry about hooks tells us why this happens

(int) (optional) Used to specify the order in which the functions associated with a particular action are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.
Default: 10

Hook priorities allow us to define the order in which our hooked functions are to be executed. So, to add our navigation bar above the footer widgets, we just execute the function with a lower hook priority than the default(10). So the code would be like

add_action( 'genesis_before_footer', 'genesis_do_subnav', 9);

And the result is what we wanted.
Screenshot from 2014-02-28 19:07:58

Other places that this might be useful in the Genesis framework:

  • Place elements(e.g. featured image) before/after entry-title, before/after entry-header, etc.
  • Change the order of elements via hooks.
  • Place elements in footer, before/ after the main footer content.
  • And many more.

If you have an interesting example, or any doubt, don’t forget to comment below, and sign up for my newsletter on the bottom to receive tutorials and articles every week.