I have a plugin which calls a stand-alone php script (myAjax.php
) via a jQuery.ajax()
script inside the plugin.
I need to place the following code into the myAjax.php
file:
require_once('../../../wp-load.php');
if (!is_user_logged_in()){
die("You Must Be Logged In to Access This");
}
if( ! current_user_can('edit_files')) {
die("Sorry you are not authorized to access this file");
}
However, I’d like a more bulletproof method of specifying the path to wp-load.php
in case the actual relative path is different than my example.
You can use
__DIR__
constant. Since the file is either inside plugin or theme folder, which are always located insidewp-content
folder.. You can simply get the path of the file and trim everything starting fromwp-content
from it:If you need to make sure the wp is not inside some
wp-content
folder (who knows? things happen) – use negative lookahead:(since it’s easier to be sure your own plugin you are developing is not located inside some other
wp-content
folder)Aaand.. your
wp-load
is there:Best Practice
As before mentioned, for AJAX you should use WordPress’ admin-ajax technique or the new REST API.
I know this is an old question but wanted to add my own answer which I think might help some users trying to achieve the same thing.
Yes it’s always better (and easier) to use the native WP Ajax API, but it can become very slow because it loads the entire WP instance.
My solution: is quite simple, and should work to retrieve the
root
of the wordpress installation. In whatever script you are doing the custom AJAX call, just make sure you first register the script withwp_register_script()
(don’t enqueue it yet). Then usewp_localize_script()
and parse theABSPATH
(this is a constant that is defined insidewp-load.php
and will hold the root path). You can now retrieve this inside your script and parse it along with the AJAX call. Finally of course make sure to actually enqueue the script withwp_enqueue_script()
.Example:
The below PHP snippet will enqueue your
script.js
file, and allows you to retrieve theroot
dir by callingpluginslug_scriptname_i18n.wp_root
. Basically thewp_localize_script()
is used to do translations, but this also becomes in handy to parse data into your script(s) that you retrieved server side.Your
script.js
could look like this:Now inside your
ajax-handler.php
you can retrieve thewp_content_dir
and load yourwp-load.php
like so:Please keep in mind that the
wp_root
can be altered client side.As a side note:
Another trick that some of you might not be aware of is that before including
wp-load.php
you can define a constant calledSHORTINIT
(boolean). This will tell WordPress to just load the basics (meaning you will lose a lot of WP core functions) but it will speed up the loading time since it won’t include all the required files for a regular WP instance. TheSHORTINIT
is defined insidewp-settings.php
(just open up the file and look forSHORTINIT
. You will have a better understanding of what is happening under the hood. This nifty trick will speed up the load times even more (up to 75% in my tests that I did some time ago). But this will depend on the WP version. Also keep in mind that thewp-load.php
changes frequently with new releases of WP versions, so if you useSHORTINIT
be sure that your script will always work even in future versions of WordPress, and also with lower version of WordPress. In short, if you do complex things that rely on a lot of the WordPress codex, then make sure to NOT setSHORTINIT
to true.I suggest this as a possible “bulletproof” method of finding the path to
wp-load.php
so long as it is in a directory above where you are running the script.You can use below code to use wp-load.php to include wp-load from any location