Changing WordPress Plugin Load Order

The Problem

You need a plugin to load before other plugins, but, by default, WordPress loads plugins alphabetically.

Breakdown

There are essentially two parts to this:

  • Plugin Load Order
  • The firing of any events related to that plugin

Plugin Load Order

The order plugins are loaded is dictated by an array stored in the wp_options table.
The option name is called ‘active_plugins‘.
When a plugin is activated:

  • It is added to the array.
  • The array is sorted alphabetically.
  • Then saved to the options table.

This all happens in /wp-admin/includes/plugin.php (lines 594-603):

594
595
596
597
598
599
600
601
602
603
	if ( $network_wide ) {
		$current = get_site_option( 'active_sitewide_plugins', array() );
		$current[$plugin] = time();
		update_site_option( 'active_sitewide_plugins', $current );
	} else {
		$current = get_option( 'active_plugins', array() );
		$current[] = $plugin;
		sort($current);
		update_option('active_plugins', $current);
	}

Immediately after this block of code a hook is fired.

606
607
608
609
610
611
612
613
614
615
616
617
618
/**
 * Fires after a plugin has been activated.
 *
 * If a plugin is silently activated (such as during an update),
 * this hook does not fire.
 *
 * @since 2.9.0
 *
 * @param string $plugin       Plugin path to main plugin file with plugin data.
 * @param bool   $network_wide Whether to enable the plugin for all sites in the network
 *                             or just the current site. Multisite only. Default is false.
 */
do_action( 'activated_plugin', $plugin, $network_wide );

Note: This hook will not fire if silently activated. See comment above hook.

With this knowledge it should be pretty easy to set an action to override the alpha sort.

Here’s a quick and dirty (and untested) override example.
Lets say we want to change the order so the WP Super Cache plugin loads first.
Here is the untested code to make that happen:

/**
 * Action callback to reprioritize plugin load order.
 *
 * @return void
 */
function boost_plugin_load_priority() {
	// Set plugin names we want to load first.
	$priority_plugins = [ 'wp-super-cache' ];
	// Get active plugin array.
	$active_plugins = (array) get_option( 'active_plugins', array() );
	// Get the priority plugins that are actually active.
	$active_priority = array_intersect( $priority_plugins, $active_plugins );
	// Perform an array union to merge the two lists.
	$prioritized_active_plugins = array_unique( array_merge( $active_priority, $active_plugins ) );
	// Save newly minted list of active plugins in proper load order.
	update_option( 'active_plugins', $prioritized_active_plugins );
}
add_action( 'activated_plugin', 'boost_plugin_load_priority' );

Change the $priority_plugins array to whatever plugins you want loaded first. You should use the plugin’s slug value as the array entry’s value.

Event Firing Order

If you want a certain plugin’s action or filter callbacks to fire before any other plugin’s callbacks on the same hook, you just need to change the priority in the add_action()/add_filter() call, which is the third parameter for both those functions.

add_action( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 )

The default priority for an action or filter call is 10. The lower the number, the higher the priority and the earlier the execution. Callbacks that have the same priority are executed in the order they were added to the action hook.

In this case:

add_action( 'my_hook', 'my_callback', 5, 1 )

The priority value of 5 being higher than the default value of 10 will generally boost the action to be run earlier than the other callbacks assigned to the my_hook hook.

Hope this was helpful!

NPM update on Windows fails to recognize new version

I’ve been trying to play with ReactJS lately, but when it came time to do an npm install on my personal Windows machine (I had been working on my work’s Macbook Pro), I received a number of errors including:

$ npm install
npm WARN package.json project-name@1.0.0 No repository field.
npm WARN optional dep failed, continuing fsevents@1.0.8
npm WARN engine is-buffer@1.1.3: wanted: {"node":">=0.12"} (current: {"node":"0.10.33","npm":"1.4.28"})
npm WARN engine is-buffer@1.1.3: wanted: {"node":">=0.12"} (current: {"node":"0.10.33","npm":"1.4.28"})
npm WARN engine is-buffer@1.1.3: wanted: {"node":">=0.12"} (current: {"node":"0.10.33","npm":"1.4.28"})
npm WARN engine is-buffer@1.1.3: wanted: {"node":">=0.12"} (current: {"node":"0.10.33","npm":"1.4.28"})
npm WARN engine is-buffer@1.1.3: wanted: {"node":">=0.12"} (current: {"node":"0.10.33","npm":"1.4.28"})
npm ERR! Error: EPERM, open 'C:\Users\username\AppData\Roaming\npm-cache\f1c9fdf5-cache-inherits-2-0-1-package-tgz.lock'
npm ERR!  { [Error: EPERM, open 'C:\Users\username\AppData\Roaming\npm-cache\f1c9fdf5-cache-inherits-2-0-1-package-tgz.lock']
npm ERR!   errno: 50,
npm ERR!   code: 'EPERM',
npm ERR!   path: 'C:\\Users\\username\\AppData\\Roaming\\npm-cache\\f1c9fdf5-cache-inherits-2-0-1-package-tgz.lock' }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.
 
npm ERR! System Windows_NT 6.1.7601
npm ERR! command "c:\\Program Files\\nodejs\\node.exe" "c:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install"
npm ERR! cwd c:\pv\www\project-name
npm ERR! node -v v0.10.33
npm ERR! npm -v 1.4.28
npm ERR! path C:\Users\username\AppData\Roaming\npm-cache\f1c9fdf5-cache-inherits-2-0-1-package-tgz.lock
npm ERR! code EPERM
npm ERR! errno 50
npm ERR! stack Error: EPERM, open 'C:\Users\username\AppData\Roaming\npm-cache\f1c9fdf5-cache-inherits-2-0-1-
package-tgz.lock'
npm ERR! not ok code 0

I’d also get something like this more often than not:

npm ERR! Linux 3.13.0-36-generic
npm ERR! argv "node" "/usr/bin/npm" "install"
npm ERR! node v0.10.32
npm ERR! npm  v2.1.0
npm ERR! path /home/user/.npm/4c25afa1-ncis-npm-vinyl-0-4-3-package-tgz.lock
npm ERR! code EEXIST
npm ERR! errno 47

To fix these errors you will need to upgrade nodejs and npm, then there are also some additional steps because …Windows.
Continue reading

Mailboxes etc

image

This is the hole in the wall where our mailboxes used to be. Mine didn’t have a lock, some others were falling apart and I am pretty sure it was older than I am. Good riddance, I say. The new management is really trying to fix things up and I am quite delighted about it. You know, aside from the fact they just cashed 8 months worth of rent that the previous management could not be bothered with. One funny thing about this picture is that it shows there used to be a wood panelled wall there that they just built another wall in front of. Gotta love urban remodeling.

News from the future

image

And so it begins. After much persuasion from my friends and family, I type this update from my brand new Samsung Galaxy Nexus. That’s right. I got a smartphone. I can’t really say that I feel smart though.

image

I picked it up during my lunch break on Monday. I walked in with the intention of just asking a few questions about data plans, but in the end it just made sense to pull the trigger then and there. This post is being written from the WordPress app on my phone. What a grand experiment. I feel like I’m living in the future.

The Addams Family Musical

A few weeks back, I was able to attend the Saturday evening showing of The Addams Family Musical at the Shubert Theater in the theater district. It was truly the last minute decision. Christy had let me know about the show and that she and some other friends would be in attendance, but I had not actually bought the ticket until a couple days prior to the show. Despite that, I still managed to get a pretty decent seat in the first row of the balcony.

Continue reading

Oh. Right. I have a blog

I’ve been pretty depressed lately.

I also recently remembered I had a personal blog that noone looks at (Google Analytics told me).

I guess that means I’m going to make an attempt to update it more often to remind myself that my life isn’t as boring and directionless as it feels.

WordCamp Boston 2011


As one might surmise by the badge above, I attended WordCamp Boston this year. It was held right down the street at Boston University. I had only found out that a WordCamp was being held in Boston about a week and a half beforehand. When I saw where it would be, I leaped at the opportunity. Registration was only US$40, which is incredibly reasonable for a technical conference.

Read on to hear more about some of the panels I attended.
Continue reading