The simplest way to require/include wp-load.php

If you want to use WordPress functionality in a PHP file that exists outside of your WordPress installation then you need to include wp-load.php. Perhaps you could call this “hooking into WordPress”.

Maybe you’re already using some sort of relative path method, like:

But this can create problems if directories change. You need a clean, dynamic way to get wp-load.php. So here is the simplest way to do it, with just two lines of code (place it at the very top of your file):


Short and sweet πŸ™‚

Disclaimer: This is intended for experimental and development purposes only. It is not advised to redundantly load WordPress on live production environments. But, why?

  1. This makes the assumtion that the WP Content folder is named wp-content. While this is true for the default install it could be named anything. This can be defined in wp_config.php itself. For my web site it is e.g. mycontent.

  2. Well, that save me some time. I was trying to make wordpress functions work on an ajaxed page. Is there some kind of shortcoming/pitfall for this method as opposed to writing an ajax function in the function.php file other than what Christian Gold mentioned?

  3. I made some simple function which can also detect path if there’s no wp-content folder.
    function wp_path() {
    if (strstr($_SERVER["SCRIPT_FILENAME"], "/wp-content/")) {
    return preg_replace("//wp-content/.*/", "", $_SERVER["SCRIPT_FILENAME"]);
    }
    return preg_replace("//[^/]+?/themes/.*/", "", $_SERVER["SCRIPT_FILENAME"]);
    }

    Usage:
    require wp_path() . "/wp-load.php";

  4. This works fairly well, but I tested this and a few other options and by far the simplest and most reliable way I found is with this one line.

    require_once(ABSPATH.’wp-load.php’);

    It works in any directory, even outside of the wp-content directory and also works if your wp-content directory has been changed.

    1. Hi Aaron, actually the method you’re mentioning here will not work.

      The ABSPATH constant is defined inside of wp-load.php itself. That’s the very file we’re trying to invoke here.

      The purpose of this post was to demonstrate how to require wp-load.php from outside of a WordPress instance. To do this, we can’t rely on any WordPress constants or functions.

      If this method is working for you, it means WordPress is already loaded, and thus invoking this file is pointless πŸ™‚

  5. Hi.

    Nice one.

    But it is including the wording “index.php” as part of the path, which is weird and lands a “Failed to open stream” error.

    Any suggestion would be of big help.

  6. Thank you for this, I have changed it slightly to make it single line.

    require_once( reset(explode( ‘wp-content’, $_SERVER[‘SCRIPT_FILENAME’] )) . ‘wp-load.php’ );

  7. My WordPress installation is in a subdomain (i.e. subdomain.example.com). If I want to call on wp-load.php in a file outside the subdomain but on the same server (i.e. http://www.example.com) what code would I need?

  8. No need to guess which directory name to use. Be it any, the following will work from it:

    require($_SERVER[‘DOCUMENT_ROOT’].’/wp-load.php’);

Leave a Reply

Your email address will not be published. Required fields are marked *