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

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

Domain Mapping for WordPress with Multisite and CPanel

I spent all of my Friday night struggling to get Domain Mapping to work so I thought I would put together a little tutorial to help others since my setup seemed unique. If not unique, not well documented!

Here’s what I have:

  • Domain name pointed to the DNS servers of my host
  • CPanel to manage my hosting
  • WordPress 3.1.1
  • Multisite has been activated with subfolders

Thats essentially it. The settings that were not really well documented involved CPanel, Multisite using subfolders, and certain aspects of the domain mapping plugin’s installation.

Continue reading for the helpful parts.
Continue reading

How to add a meta-box to an admin page in WordPress

I’ve been developing a plug-in for Powet.TV to automate some background stuff that we do regularly.
One of the things I need to do is add a meta-box to the ‘edit post’ administration page. The below image is an example of a very simple meta-box. Its just a standard container for content in the administration panel.

This wasn’t as straight-forward as I expected it to be, or I should say, there weren’t any examples that were as straight-forward as I was hoping there would be. The best tutorial I found was located here. The problem with this tutorial was that it didn’t spell it out clearly enough for me. It was doing a few different things in the example and I just wanted a very simple meta-box without any functionality to start. My functionality was guaranteed to not match the functionality of the one in the example.

Continue reading

Ubiquity


Ubiquity for Firefox from Aza Raskin on Vimeo.

Mozilla recently revealed an add-on for Firefox called Ubiquity. Basically it serves as a command line for your browser. The catch is that most of the commands are created by users and creating commands is very simple. This allows people to flex their creative muscle and take advantage of current API‘s that are available for different sites and create really useful (and more often useless) mashups. If you watch the above video, you can see a few examples of the mashups I’m talking about. Having recently moved into a new apartment, I can fully appreciate the example where he mapped several craigslist apartment listings on a single map with just a few keystrokes. If I ever find some free time, I’d really like to explore some of the things you can do with this and whats already out there.

Direction

This weekend I made a conscious decision about what I want to be doing.

Now don’t get me wrong, I like what I’m doing right now, but I’m being pulled in multiple directions right now and I feel like I’m starting to come apart at the seams. I need some ‘synergy’.

When I was a system administrator at my last job, I was usually working on some sort of webpage to help me out or on this blog. And I did spend a significant amount of time on this blog. Then I started my current job, which works mostly in Java, which I do not know. I work mostly in VB. We do have several intranet webpages that help automate tasks and streamline processes. I’ve really taken to working on those quite a bit. Since I’ve been here, I’ve also started Powet.TV with Zac.

You can see the pattern here. Web programming.

This is an area I am very interested in, but have let my skill set become stagnant. I need to improve and read up on several areas of technology and devlopment, but I think concentrating in this area at this time is the most beneficial option to me and hopefully I can leverage it at work, Powet, and right here on my blog.

So that being said, I went shopping on Amazon.com for some new books. Here’s what I ordered:

Hopefully, these won’t come in and then sit on a shelf for 3 years before I pick one up …because that never happens….