25

In what order are plugins loaded in WordPress?

And within a particular plugin's folder, what order is followed for loading?

28

Answer to the First question:

  1. In wp-settings.php, WordPress first checks for any must-use plugins (plugins in the optional mu-plugins folder) and loads those.

  2. Then, if you're running a multisite installation, it checks for plugins that are network-activated and loads those.

  3. Then it checks for all other active plugins by looking at the active_plugins entry of the wp_options database table, and loops through those. The plugins will be listed alphabetically.

Here's the order WordPress loads pretty much everything: http://codex.wordpress.org/Action_Reference#Actions_Run_During_a_Typical_Request

The thing is, it usually doesn't matter what order each individual plugin is loaded in, because properly-written plugins will use WordPress hooks, which let you plug in functionality into specific points later in the WordPress startup. (Properly-written plugins will also prefix their functions and classes so that there aren't any conflicts.)

More info on plugin API: http://codex.wordpress.org/Plugin_API/

Answer to the Second question:

Totally depends on the plugin. WordPress only loads one file in the plugin, the one that's usually named the-plugin-name.php and contains the title, description, author, etc. at the top. It's up to the plugin to load the rest of its files, using require_once and wp_enqueue_script and whatnot.

| improve this answer | |
  • So if I create a plugin, then later release an add-on plugins that should run after the original plugin is run, how can I ensure that order? – urok93 Aug 27 '12 at 6:21
  • 1
    It really depends what you're trying to do. But generally you would set up each of them to run on a specific hook using the add_action() function, which allows you to set priority. codex.wordpress.org/Function_Reference/add_action – SeventhSteel Aug 27 '12 at 13:31
  • @SeventhSteel But what if a plugin relies on the hook created by another plugin. If one plugin uses apply_filters() to create a hook, but runs before a plugin that uses add_filter(), then will it still work? Logically, it seems that apply_filters() will have fired already, so the add_filter() calls in the second plugin won't do anything. Does it matter in what order the plugins load when using only plugin-created hooks? – trusktr Jul 1 '13 at 17:15
  • What matters is what has loaded by the time apply_filters() runs. add_filter() must run before apply_filters(). If add_filter() is in a plugin file and not in another function, it will run on the plugins_loaded hook which is generally going to happen before you'd need apply_filters(). – SeventhSteel Nov 21 '14 at 17:28
  • A common scenario is having several plugins that append their content after the_content -- how is the order of their appearance determined? – Dan Knauss Mar 23 '17 at 16:12
12

I find that it is useful to include a 'late loading' action in my plugin that runs after all plugins have completed their load like this:

add_action('plugins_loaded', 'my_late_loader');

The function my_late_loader then is initiated after all other plugins which allows me to use hooks defined by other plugins in my own function my_other_function which is initiated within my_late_loader

/**
 * Late loading function for actions that must run after all plugins 
 * have loaded
 */
function my_late_loader(){
    add_action( 'some_hook', 'my_other_function', 10, 1);
}

Bit convoluted (I know), but this ensures that hooks in other plugins are created before being added irrespective of the plugin load order.

| improve this answer | |
0

I originally came to this page with the same question as the first one. Both the answers by @SeventhSteel and @Clinton provide some very useful data, but require additional digging. As stated in those answers, your properly written plug-in will use hooks, and will have a call something like the following):

add_action('init', 'function_to_add');

The first parameter is "the name of the action" to which your function -- the second parameter -- is hooked. The action specified is the main determinant of when your plugin is loaded. There is an optional third parameter (priority) which gives you some granular control over where within that group your function is loaded. (Though this is hardly precise since it would often be difficult to know the priorities of all installed plugins.) There is an excellent list of all the available action hooks with explanations thereof for various request types in the WordPress Codex.

As for the second question, as stated in the Plugin Basics documentation:

When WordPress loads the list of installed plugins on the Plugins page of the WordPress Admin, it searches through the plugins folder (and its sub-folders) to find PHP files with WordPress plugin header comments.

So the file in your plug-in with the header (per the documentation, there should be only one) will be loaded first. See @Clinton's answer for suggestions on handling multiple hooks within a single plug-in.

Should you come to this answer (as I did) as a first-time WordPress plugin developer, there's a lot more useful information to be found in WordPress' Plugin Handbook.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.