0

I'm working on a quick plugin for my work and what I need is for my plugin to load as early as possible as it uses ob_start to buffer everything that is going on. I can make the plugin load as the first one:

function my_plugin_load_first()
{
    $path = str_replace( WP_PLUGIN_DIR . '/', '', __FILE__ );
    if ( $plugins = get_option( 'active_plugins' ) ) {
        if ( $key = array_search( $path, $plugins ) ) {
            array_splice( $plugins, $key, 1 );
            array_unshift( $plugins, $path );
            update_option( 'active_plugins', $plugins );
        }
    }
}

Which works fine in normal circumstances. But the issue arises when cache is enabled as it loads very early on

E:\Programs\xampp\htdocs\wordpress\wp-includes\plugin.php
E:\Programs\xampp\htdocs\wordpress\wp-includes\class-wp-hook.php
E:\Programs\xampp\htdocs\wordpress\wp-includes\version.php
E:\Programs\xampp\htdocs\wordpress\wp-content\advanced-cache.php
E:\Programs\xampp\htdocs\wordpress\wp-content\plugins\wp-super-cache\wp-cache-phase1.php
E:\Programs\xampp\htdocs\wordpress\wp-content\plugins\wp-super-cache\wp-cache-phase2.php
E:\Programs\xampp\htdocs\wordpress\wp-content\wp-cache-config.php

Pretty much right after plugin.php, while the rest of the plugins are way down there. I can't seem to find how it had been done and if it can even be altared as it may perhaps be hardcoded into wordpress to detect cache plugins and automatically assign them to be loaded early on. Perhaps I can make my script to be seen as such.

  • Have you heard of Must Use Plugins? They get placed in a special directory, can't be switched on or off (they always are on), and get loaded before all other plugins. Is it this you are looking for? – leymannx May 10 '19 at 21:36
0

By default WordPress first checks for any must-use plugins (plugins in the optional mu-plugins folder) and loads those. So the first plugins loaded will be those located in /wp-content/mu-plugins folder

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

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 is info about the order WordPress loads everything: https://codex.wordpress.org/Action_Reference#Actions_Run_During_a_Typical_Request

You can learn more about WordPress plugin API here: https://codex.wordpress.org/Plugin_API/

Answer:

So if you want to load that specific plugin first, then create /wp-content/mu-plugins/ folder and copy your plugin inside it, making sure it is first in alphabetical order

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.