WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:


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.

share|improve this question
up vote 5 down vote accepted

To add to what Milo said, here are 2 great posts on the subject.
http://www.garyc40.com/2010/03/5-tips-for-using-ajax-in-wordpress/ (last version ) http://ottopress.com/2010/dont-include-wp-load-please/ (last version )

Also for the sake of learning, the first slideshow http://andrewnacin.com/2011/04/16/wordcamp-seattle/ (last version )

share|improve this answer
He wast ask what not to do, question was simple-how get wp-load.php address. – Tommixoft Sep 30 '14 at 14:09

Don't. Use AJAX the proper WordPress way.

share|improve this answer

You can use __DIR__ constant. Since the file is either inside plugin or theme folder, which are always located inside wp-content folder.. You can simply get the path of the file and trim everything starting from wp-content from it:

$path = preg_replace('/wp-content.*$/','',__DIR__);

If you need to make sure the wp is not inside some wp-content folder (who knows? things happen) - use negative lookahead:

$path = preg_replace('/wp-content(?!.*wp-content).*/','',__DIR__);

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



As guys before mentioned, for AJAX you can use WP-s native ajax technique.

Of course, there are cases when WP's native AJAX technique is not enough.

share|improve this answer
wp-content can be absent or in a completely different directory than WP. – toscho Jun 3 '15 at 18:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.