Hey, @islp! Good question.
Since the hook is part of the plugin, you can use it after the plugin has been initialized. This means you need to ensure your calls are executed after the plugins_loaded hook is triggered.
Thread Starter
islp
(@islp)
@igortron ok, but I’m not sure this covers all the possible scenarios (that is maybe my plugin under some circumstances fires before all the plugins are loaded)
Got it. That is exactly what WP hooks were created for.
When you definitely call it before plugins_loaded fired, you have to hook on it
add_action( 'plugins_loaded', function() {
do_action( 'logger' );
} );
But if you arn’t sure whether your call runs before or after it, you could add an extra check
$do_log = function () {
do_action( 'logger' );
};
// Check if the action 'plugins_loaded' has not been fired,
// then perform logging on this action.
if ( ! did_action( 'plugins_loaded' ) ) {
add_action( 'plugins_loaded', $do_log );
} else {
$do_log();
}
Thread Starter
islp
(@islp)
@igortron Interesting solution, thanks 🙂
Another option is to turn this Logger into a “must use” plugin. That way, its API becomes available before any regular plugins are loaded.
Thread Starter
islp
(@islp)
@igortron This is what I’ve done recently for my plugins: I have many custom plugins in the same website and ANY plugin uses a bunch of identical static methods. I moved any methods to mu-plugins and removed the local copies in any single custom plugin directory (avoided duplication)
-
This reply was modified 1 week, 3 days ago by
islp.