2

I currently have a global function in my functions.php file

ein_error_log($message)
{
      //push out $message to file... 
}

But I want to start using it in my MU-Plugin directory and it doesn't know it exists. So I assume it's because MU-Plugins folder is read before the theme folder in WordPress's hierarchy, which makes sense.

But if the hierarchy is the case, how do I distinguish which MU-Plugin runs first to make sure all other MU-Plugin files can run dependent on said file?

-- UPDATE--

I have my ein_error_log($message) method transferred into an mu-plugin file. I realized how silly it was to have mu-plugin files dependent on a method inside function.php file. But I still wanted the other mu-plugin files to also use this error logging method. This requires some mu-plugins to have a dependency. Its small, but important to me.

2
  • I don't think this is possible, because of loading sequence. you need to do manual hacks, like having all your common functions in a plugin and make sure this plugin loads first, but this doesn't make sense, or you have to define the function twice in both of plugin and theme but make sure the one within the theme is preceded with a check if(function_exists('function_name)) to prevent confliction and make sure the function will work if the plugin is inactive.
    – Makiomar
    Commented Aug 9, 2019 at 18:43
  • I dont see the point in defining the function a second time in my theme folder. I have access to my plugin defined functions inside the theme folder.
    – jonnyK
    Commented Aug 9, 2019 at 20:03

2 Answers 2

0

Thanks to https://salferrarello.com/functions-plugin-mu-plugin-wordpress/

I was able to confirm when Code Runs

All of the code across the different files of WordPress can be thought of as one big list of instructions. The code is run line by line starting at the beginning of this list through to the end of this list. The order is essentially.

  1. the WordPress
  2. core code
  3. mu-plugins
  4. plugins
  5. functions.php
  6. the theme code for the specific template being displayed

But it doesn't answer the order MU_plugins are run (alphabetical, order of importance?)

So my currently solution was to add

include('/my-functions.php');

to any MU-Plugin file that uses '../mu-plugin/my-functions.php'. It works.

0

I'd say generally you'd want to run as little code as possible right from your plugin file. It is way better to use hooks like muplugins_loaded or plugins_loaded, the later the better. This way code only runs after all plugins are initialized which means you don't need to worry about load order any more.

1
  • Thanks! I actually don't keep too much code in my plugins files. Those hooks will come in handy for sure!
    – jonnyK
    Commented Aug 15, 2019 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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