Get your priorities straight: Fun with the WordPress "title tag" feature

No Comments

One for my fellow, work-beaten, WordPress developers:

While working on a wordpress-related job today, ie. a theme based on _s, I kept bumping into the issue of "the bloody title tag is missing!". The information in the codex did reveal exactly NOTHING, and after a few extensive searches, I was mostly none the wiser. But: A few comments in the BuddyPress bug tracker pointed towards issues with action hooks and their priority settings, ie. at what point which function is being called.

So I first fiddled around with the inner action hooks (add_action), which are defined inside the class constructor. Those include the setup method of the theme, too, which is set to after_setup_theme. Neither reordering, nor changing some of them did help. Finally I just set the priority of the class initialization hook from nothing (which defaults to 10) to 1 - with instant results = title tag worked again :)

Long story short: If you're using a class for better code encapsulation (and OOP!), and run into this issue, set the priority of the hook to 1. That lets it run at the very start, and enables other callback functions / methods, that might be bound to after_setup_theme, to fire as well ;)

A more thorough example with a separate priority variable, to highlight the fix:

$init_priority = 1;	
add_action('after_setup_theme', array( 'my_theme_class', 'init'), $init_priority );

Normally, you would just set the priority straight in the add_action function call.

Be the first to write a comment!