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):
1 2 3 4 5 6 | <?php $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?
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 inwp_config.php
itself. For my web site it is e.g.mycontent
.Frankie Jarrett
July 18, 2012 — 3:43 pm
Yes, that’s correct. Thanks for the feedback!
Grant Landram
December 10, 2012 — 6:08 pm
Having the same issue (mine is called “content”). Any fix for irregular wp-content naming or locations?
ikkeaviy
October 29, 2012 — 2:19 am
where will i put the code?in what file?
Frankie Jarrett
October 29, 2012 — 12:47 pm
Whichever file you want to load that is outside of the WordPress templates. It must be a PHP file.
sy
November 2, 2012 — 9:51 am
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?
sy
November 2, 2012 — 9:53 am
Nevermind I read that disclaimer.
peyman
February 14, 2013 — 6:26 am
Thank you very much
I am looking for this for a long time!!!!
Nikita
February 20, 2013 — 9:56 am
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";
MED
March 17, 2013 — 11:06 pm
Thank you very match 😉
Lori
March 25, 2013 — 5:13 pm
You saved my sanity when my slider-builder wasn’t loading!
Thank you – excellent advice!
Daniel Lemes
June 10, 2013 — 11:49 pm
Thank you, really “short and sweet”.
Deepak
September 30, 2013 — 5:05 am
thank you so much!!!!!
Naved Ahmed
December 18, 2013 — 4:53 am
A big thanks to you mate… 😀
Aaron Milgrom
February 18, 2014 — 4:50 pm
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.
Frankie Jarrett
February 19, 2014 — 12:45 am
Hi Aaron, actually the method you’re mentioning here will not work.
The
ABSPATH
constant is defined inside ofwp-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 🙂
Saleh
April 28, 2014 — 10:50 pm
Works Perfectly Fine Thanks. I was Almost about to Hard Code the url ,You Saved me 😀
Juan
May 23, 2014 — 1:56 pm
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.
John
August 14, 2014 — 2:53 pm
Hey Juan if you’re having trouble with
$_SERVER['SCRIPT_FILENAME']
use__FILE__
instead.Tomas Samot
September 30, 2014 — 9:12 am
Wrong POST, bad ideas and misleading others. My all installations dont have wp-content directory, so this POST wont work.
Richard Leishman
May 16, 2015 — 8:45 am
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’ );
Gino
December 27, 2015 — 4:38 pm
it do not work, use instead
require_once( $_SERVER[‘DOCUMENT_ROOT’].’/wp-load.php’ );
Marcio Duarte
April 2, 2016 — 2:06 pm
Gino’s solution was the only one that worked for me:
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );
Steve Younis
May 4, 2016 — 5:45 pm
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?
Sergio
July 27, 2016 — 10:52 pm
Muchas gracias amigo por el aporte!
wecode
August 31, 2016 — 8:05 pm
You saved my neck… I was struggling with this for whole day …and you make my day. Your solution worked well
Darshan Saroya
December 30, 2017 — 12:29 am
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?