0

I have the plugin which have constant defined, i will call this plugin PLUGIN A:

if ( ! defined('BLA')){
    define('BLA', 'http://google.com); 
}

I want to make another plugin PLUGIN B which will define constant BLA before PLUGIN A. Is that possible? I guess Wordpress needs to read PLUGIN B before PLUGIN A, but in which order Wordpress is reading plugins?

2
  • 1
    From the linked answer "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." – kero Sep 14 '17 at 9:14
  • Even I could place PLUGIN B in mu-plugins (must use) I prefix plugin name, folder and main PHP file with "aa" (aaPLUGIN B). In that way aaPLUGIN B was loaded before PLUGIN A. I don't care about notices what constant is already defined, so this way worked for me. – gardelin Sep 14 '17 at 9:58
4

Defining constants on the global scope instead of using WordPress hooks, the first plugin loaded (See answer linked by Kero) will get the chance to define the constant.

However, you could use some of the API hooks in your plugin, the earliest one possible is plugins_loaded, and then you can set priorities for the callbacks hooked into this so you can have the callback from plugin B executed before the callback from plugin A:

// code in plugin B
add_action('plugins_loaded', function(){
    if ( !defined('BLA') ) {
        define ( 'BLA', 'http://google.com' );
    }
}, 0); # <= 0 is the priority

// code in plugin A
add_action('plugins_loaded', function(){
    if ( !defined('BLA') ) {
        define ( 'BLA', 'http://google.com' );
    }
}); # <= priority is 10 by default.

That constant will then be accessible in any scope hooked into a WordPress action hook (init, plugins_loaded, wp, etc..).

// any other plugin
add_action('plugins_loaded', function(){
    echo BLA, PHP_EOL; # "http://google.com" hopefully
}, 11);

Hope that helps.

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