Case of main filters/actions during initialisation of WP [reserved for developers]

[note: example with jetpack was based on version 2.3. now recent jetpack 2.4 have changed initialization according better rules explained here]

What you need to know before adding filters in plugin or theme’s file functions.php ?

The case of main filters/actions during initialisation of WP.

The time line is very visible if your read the wp-settings.php file line by line.

Launching (do_action) of plugins_loaded (#211) preceeds that of init (#306). And both occur after plugin files including loop. (#196)

196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Load active plugins.
foreach ( wp_get_active_and_valid_plugins() as $plugin )
	include_once( $plugin );
unset( $plugin );
 
// Load pluggable functions.
require( ABSPATH . WPINC . '/pluggable.php' );
require( ABSPATH . WPINC . '/pluggable-deprecated.php' );
 
// Set internal encoding.
wp_set_internal_encoding();
 
// Run wp_cache_postload() if object cache is enabled and the function exists.
if ( WP_CACHE && function_exists( 'wp_cache_postload' ) )
	wp_cache_postload();
 
do_action( 'plugins_loaded' );

Why it is important to understand filter priority during enqueuing ?

The things – including files, instancing classes, adding filter (action) – need to be done one after the other. They must give (allow) place to be modified by another plugin. Doing one of these things inside the step where plugin files are being included is not efficient and the order is only related to name of plugins and it difficult or impossible for another plugin to modify installed filters by the previous plugin. Because adding a specific filter for a specific plugin is uneasy to maintain, it is far better to follow time line and standard filter (action).

What can we learn from the launch of jetpack plugin – latest lines of jetpack.php ?

580
581
582
583
584
585
// line 4580 and...
add_action( 'init', array( 'Jetpack', 'init' ) );
add_action( 'plugins_loaded', array( 'Jetpack', 'load_modules' ), 100 );
add_filter( 'jetpack_static_url', array( 'Jetpack', 'staticize_subdomain' ) );
 
Jetpack_Sync::sync_options( __FILE__, 'widget_twitter' );

We find two main filters (action), init and plugins_loaded… And what happen in wp-settings.php

Launching (do_action) of plugins_loaded (#209) preceeds that of init (#306).

At end of jetpack.php a function calling Twitter is fired when including plugin file (#194) ( Jetpack_Sync::sync_options( FILE, ‘widget_twitter’ ); ). This function instanciates at this time the jetpack class. So the (previous) init filter seems to be redundant and explains some difficultes with load_plugin_textdomain step. So jetpack plugin architecture need cleaning works !

About functions and objects added in functions.php of current theme.

286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
$GLOBALS['wp_locale'] = new WP_Locale();
 
// Load the functions for the active theme, for both parent and child theme if applicable.
if ( ! defined( 'WP_INSTALLING' ) || 'wp-activate.php' === $pagenow ) {
	if ( TEMPLATEPATH !== STYLESHEETPATH && file_exists( STYLESHEETPATH . '/functions.php' ) )
		include( STYLESHEETPATH . '/functions.php' );
	if ( file_exists( TEMPLATEPATH . '/functions.php' ) )
		include( TEMPLATEPATH . '/functions.php' );
}
 
do_action( 'after_setup_theme' );
 
// Set up current user.
$wp->init();
 
/**
 * Most of WP is loaded at this stage, and the user is authenticated. WP continues
 * to load on the init hook that follows (e.g. widgets), and many plugins instantiate
 * themselves on it for all sorts of reasons (e.g. they need a user, a taxonomy, etc.).
 *
 * If you wish to plug an action once WP is loaded, use the wp_loaded hook below.
 */
do_action( 'init' );
 
// Check site status
if ( is_multisite() ) {
	if ( true !== ( $file = ms_site_check() ) ) {
		require( $file );
		die();
	}
	unset($file);
}

This file is included after plugins file and roles defined. The file of the child theme is inserted after that of the parent theme. (# 287), and then launching ( (do_action) after_setup_theme (#294) preceeding init (#306). It is therefore possible to modify plugin behaviour or filters inserted by plugins. All here will only activate if this theme is the current. When reading wp-settings.php, we see that only filters (present in wp-settings) like ‘init’ can be used or modified (associated function changed or his priority…). Note that in case of child theme, the priority of after_setup_theme must set to 11. So the functions fired by the ‘child’ filter will be executed ‘after’ those of parent.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*