admin管理员组

文章数量:1202861

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.

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.

Share Improve this question asked May 10, 2019 at 20:20 Just Call me SteveJust Call me Steve 1 1
  • 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? – norman.lol Commented May 10, 2019 at 21:36
Add a comment  | 

1 Answer 1

Reset to default -1

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:

Update: The real solution could be a Drop-in plugin, their priority is above mu-plugins and plugins.

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

本文标签: phpLoad my plugin before cache