I want to run a script before any Wordpress stuff kicks in.

I know it's wrong but currently I have my function in the index.php file before the below code:


require 'vendor/autoload.php';

use GeoIp2\Database\Reader;


if (!isset($_SESSION['country'])) {
    $reader = new Reader('db/GeoLite2-Country.mmdb');

    $record = $reader->country($_SERVER['REMOTE_ADDR']);

    $_SESSION['country'] =  $record->country->isoCode;


* Front to the WordPress application. This file doesn't do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 * @package WordPress

* Tells WordPress to load the WordPress theme and output it.
* @var bool

define( 'WP_USE_THEMES', true );

/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/wp-blog-header.php' );

This obviously gets overidden whenever Wordpress updates so how can I include my function safely and correctly please?

I've tried with the following in my themes functions.php file:

add_action( 'init', 'my_script' );

function my_script() {

      // my script here

But that's not soon enough, unless I'm making a mistake?

Can anyone help please?

Many thanks

  • 2
    The earliest hook I can think of is muplugins_loaded. Have you tried this one? – Jack Johansson Jun 25 at 18:39
  • What does your script do? Why does it need to be run before everything else? Is there a technical reason? – Jacob Peattie Jun 26 at 1:13
  • I want to initialise a session so it needs to be before the headers are sent. – Ordog Jun 26 at 14:59

init is not the first action to run on a WordPress install. Here is a basic rundown of a typical stack order.


Try hooking into `muplugins_loaded'. I don't believe it is conditional, so it should run every time.


  add_action( 'muplugins_loaded', 'my_script' );

  function my_script() {
    // my script here
  • Ahh ok thank you, I'll give that at try later. I'm trying to initalise a session so my function needs to run before the headers are sent. – Ordog Jun 26 at 15:00
  • I tried this and USE cannot be used within a function so it doesn't work. I've updated my original question to include the function that I want to run. Hopefully you can help – Ordog Jul 1 at 21:01

The first PHP file that WordPress loads and doesn't overwrite on update is wp-config.php. At this point, WordPress is not loaded. So you can put your functions at the beginning of that file.

  • I've updated my original question to include the function that I want to run. Does this help in any way or would you still suggest that I add in in the wp-config file? – Ordog Jul 1 at 21:03

You can simply place your code in any file (with a .php extension that is) in /wp-content/mu-plugins/

All PHP files in that directory (but not subdirectories) will automatically be run before any hooks are fired (yes even before muplugins_loaded.)

This is arguably a better place for sitewide customizations than a theme functions.php anyway so they will persist if you change themes.

  • I tried that and whatever .php file I include in that directory (even an empty one) causes the site to error. I've updated my original question to include the function that I want to run. Hopefully you can help – Ordog Jul 1 at 21:02
  • Check the file owner/group permissions on the mu-plugins directory to make sure they match the rest of your installation (since you probably just created it). There is no good reason for auto-included must-use plugins files to just fail. If it is not this, you can check your error logs to find out what the error is more specifically. – majick Jul 3 at 6:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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