I want to add a hook that is the first thing that runs every and any time someone hits a page, post or other WP piece of content on my site, and before anything else in WordPress executes.

Can you help me with what that hook should look like?

Is it init ?

Does it look like this: add_action('init', 'callMyFunction');

Or this: do_action('init', 'callMyFunction');

Thank you.


1 Answer 1


The earliest hook accessible from external code is muplugins_loaded. In order to use it, create a directory mu-plugins in your wp-content directory and put a PHP file into that directory.

Sample code, will have side effects(!):

add_action( 'muplugins_loaded', function() {
    print current_filter();

This is, of course, not "before anything else in WordPress executes". WordPress has to run some code to load such a mu-plugin. You can find the hook it in /wp-settings.php. Right above that call, you can see what happens before.

The real question here is why you think you need to run code so early. This is almost never needed.

If you want to require a log-in for some sites, wait for plugins_loaded (is_user_logged_in() doesn't work earlier) and do something like this:

add_action( 'plugins_loaded', function() {

    if ( is_user_logged_in() )

    $protected_sites = [1, 4, 7];

    if ( in_array( get_current_blog_id(), $protected_sites ) )
  • Thank you for helping and clarifying @toscho. I am trying to put my entire site behind a firewall. I want to force any and every visitor to the site to have to login into WP before being able to see any page, post, or other piece of content. [FYI Note: this site is not for wide public consumption. It is restricted to a handful of authorized users]
    – tammy
    Jul 17, 2015 at 14:16
  • @tammy Like this solution?
    – fuxia
    Jul 17, 2015 at 14:18
  • Also, thanks for suggesting making it an MU plugin. I can't go that route because I am running a Multisite network and I only want 1 of my sites to be behind this firewall, not every site in my network.
    – tammy
    Jul 17, 2015 at 14:18
  • 1
    @tammy See my update. I have added an example that should solve your use case.
    – fuxia
    Jul 17, 2015 at 16:41
  • 1
    @tammy Everything that is routed through WP. Not single media files, they are accessed directly.
    – fuxia
    Jul 17, 2015 at 18:26

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.