Change WordPress Plugin loading priority

If you developing a plugin that have dependency of other plugin, the sequence of your plugin loading sometime might be crucial. For example, you want the object/data loaded first in your dependent plugin, so that you can use it afterward, and no need to reinitialize the instance in your plugin.

Here is how you can do it, to put your plugin loaded last:

add_action("activated_plugin", "my_plugin_last");
function my_plugin_last() {
	$plugin_path = preg_replace('/(.*)plugins\/(.*)$/', WP_PLUGIN_DIR."/$2", __FILE__);

	$my_plugin = plugin_basename(trim($plugin_path));//my plugin path

	$active_plugins = get_option('active_plugins');//get the activated plugin list

	$my_plugin_key = array_search($my_plugin, $active_plugins);//search the position of my plugin in the active plugin list array

    array_splice($active_plugins, $my_plugin_key, 1);//remove your plugin in the original active plugin list.

    array_push($active_plugins, $my_plugin);//push your plugin to the last one of the plugin list array

    update_option('active_plugins', $active_plugins);//update the new plugin list
}

I am using activated_plugin action hook for this situation. Of course you can put your plugin in the first priority for whatever reason, you can do it as well, just replace array_push to array_unshift function instead.

Leave A Comment