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

$priority
(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.

Center – A Free Genesis Child Theme

Hi there!! After working with Genesis for a few months (on client websites), I’m releasing my first child theme, Center. It’s a very minimalistic, yet elegant, one column blog theme. You can see a few screenshots below.

Some features of the theme include

  • Single column
  • One widget area below site title.
  • Widget area below primary nav
  • Two widget areas in footer
  • Customizable footer text.
  • Two color schemes, light and dark.

The theme is released under GPL, and the code is on github. Please test it, report bugs, if any, and suggest improvements.

You can check out the theme here.

Download Now

Hello World!

If you’re reading this, you either followed a link on one of the social media websites, or you know me from somewhere. If you know me already, high five. If you don’t know me, you will.

After running 3 failed startups, and a moderately successful design agency during the past 5 years, with different partners, I’m on my own in the wild now. MagikPress is my first foray into the world alone, and I hope that this will turn out to be a positive thing.

Anyways! This will be the official MagikPress blog, and I’ll write things related to WordPress, Genesis, Bootstrap, Linux and various things, ranging from tutorials to reviews to full blown product launches.

For the first few weeks, I’ll be taking requests for articles. So if you have anything that you want me to write about, do mention in the comments. Topics that I’ll cover are:

  • WordPress(themes, plugins, development)
  • Genesis Framework
  • Design, typography, UX and things like that.

P.S. I’m testing my first Genesis child theme right now, so stay tuned for that.