I am creating my very first Wordpress plugin. I intend to make this a wrapper, so it should be initialized before other elements are initialized and finalized after other elements are finalized.
I have been researching the actions here and thought muplugins_loaded and shutdown should be the right events for my scenario.
muplugins_loaded
is supposed to be executed
After must-use plugins are loaded
so, I have a code like this:
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
//Checking whether queries are to be saved
if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) {
//definition of the WordpressBenchmarker class
function initWordpressBenchmark() {
WordpressBenchmarker::initialize();
}
function finalizeWordpressBenchmark() {
WordpressBenchmarker::finish();
}
add_action( 'muplugins_loaded', 'initWordpressBenchmark', 1 );
add_action( 'shutdown', 'finalizeWordpressBenchmark', 20 );
}
I get this error:
Call to a member function finalize() on a non-object in
which clearly points to this line:
WordpressBenchmarker::finish();
which, believe me that surely means that initWordpressBenchmark
was not executed. This makes me think that the plugin was not executed because it is not a must used plugin. Should I make it a must used plugin? Is there a better way? As an alternative I am thinking using the plugins_loaded
hook with a value which is smaller than any other values. After searching for references for plugins_loaded
I have seen that the smallest number for priorities was 0. I am wondering whether a negative value as the priority
add_action( 'plugins_loaded', 'initWordpressBenchmark', -1 );
would be a good solution.
muplugins_loaded is not triggered for not must used plugins. It would be an unorthodox approach to make my benchmarking plugin a must used plugin, since benchmarking is not a must used feature by definition, but rather a plugin which needs to be used when optimization is needed. So, I have decided to not make my plugin a must used plugin. Instead of that, I have modified my hook to be triggered upon plugins_loaded
instead of muplugins_loaded
. I have added the hook with a priority of -1 and have used a low priority for the shutdown
hook. This apparently solved the issue:
add_action( 'plugins_loaded', 'initWordpressBenchmark', -1 );
add_action( 'shutdown', 'finalizeWordpressBenchmark', 1001 );