Watch those hook priorities
When adding an action or a filter to WordPress, the third parameter allows you to adjust priority. In this way, multiple bits of code can determine the order that they should execute without having to rely on the order that PHP actually parses them.
Generally, there are no minimums or maximum limits (other than those imposed by PHP itself) for priority, but there’s a very good reason to give consideration to when your code runs. Here’s an example:
Widget registration should be hooked to ‘widgets_init’ as in the following code:
function mytheme_register_widgets() { register_widget( 'Mytheme_Widget' ); } add_action( 'widgets_init', 'mytheme_register_widgets' );
This is also the hook used internally by WordPress to take all the registered widgets and create a global array, $wp_registered_widgets
. Specifically, this is in the WP_Widget_Factory class’s constructor function which is called once in wp-settings.php, immediately before the ‘setup_theme’ action hook.