How to include wp-load.php from any location?

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:

include($path.'wp-load.php');

But!

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.

Leave a Comment