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:

<?php include '../../../wp-load.php'; ?>

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):

$parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );
require_once( $parse_uri[0] . 'wp-load.php' );

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?


Add yours →

  1. Christial Gold

    July 18, 2012 — 3:12 pm

    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. where will i put the code?in what file?

  3. 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?

  4. Nevermind I read that disclaimer.

  5. Thank you very much
    I am looking for this for a long time!!!!

  6. 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"]);

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

  7. Thank you very match 😉

  8. You saved my sanity when my slider-builder wasn’t loading!
    Thank you – excellent advice!

  9. Thank you, really “short and sweet”.

  10. A big thanks to you mate… 😀

  11. 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.


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

    • 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 🙂

  12. Works Perfectly Fine Thanks. I was Almost about to Hard Code the url ,You Saved me 😀

  13. 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.

  14. Hey Juan if you’re having trouble with $_SERVER['SCRIPT_FILENAME'] use __FILE__ instead.

  15. Wrong POST, bad ideas and misleading others. My all installations dont have wp-content directory, so this POST wont work.

  16. 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’ );

  17. it do not work, use instead
    require_once( $_SERVER[‘DOCUMENT_ROOT’].’/wp-load.php’ );

  18. Gino’s solution was the only one that worked for me:

    require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );

  19. My WordPress installation is in a subdomain (i.e. If I want to call on wp-load.php in a file outside the subdomain but on the same server (i.e. what code would I need?

  20. Muchas gracias amigo por el aporte!

  21. You saved my neck… I was struggling with this for whole day …and you make my day. Your solution worked well

  22. The team from WordPress say it is not a secure way to include wp-load.php file in a plugin. How can I include it securely?

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


Leave a Reply

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

%d bloggers like this: