admin管理员组文章数量:1122846
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.
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.
- 1 Don't. Use AJAX the proper WordPress way. – Milo Commented Aug 18, 2011 at 16:00
- To add to what Milo said, here are 2 great posts on the subject. garyc40.com/2010/03/5-tips-for-using-ajax-in-wordpress (last version ) ottopress.com/2010/dont-include-wp-load-please (last version ) Also for the sake of learning, the first slideshow andrewnacin.com/2011/04/16/wordcamp-seattle (last version ) – Wyck Commented Aug 18, 2011 at 16:17
- 1 You should use a REST API endpoint instead of a standalone file so you get a nice pretty URL and don't have to load/bootstrap WP. Standalone PHP files like this are a security risk and carry lots of problems – Tom J Nowell ♦ Commented Oct 3, 2022 at 11:49
5 Answers
Reset to default 11You 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:
require_once( $path . 'wp-load.php' );
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 with wp_register_script()
(don't enqueue it yet). Then use wp_localize_script()
and parse the ABSPATH
(this is a constant that is defined inside wp-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 with wp_enqueue_script()
.
Example:
The below PHP snippet will enqueue your script.js
file, and allows you to retrieve the root
dir by calling pluginslug_scriptname_i18n.wp_root
. Basically the wp_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.
$handle = 'pluginslug-scriptname'; // Set script handle
$name = str_replace( '-', '_', $handle ) . '_i18n'; // Will convert handle to pluginslug_scriptname_i18n
wp_register_script( $handle, plugin_dir_url( __FILE__ ) . 'script.js', array(), '1.0.0', false );
wp_localize_script(
$handle,
$name,
array(
'ajax_url' => plugin_dir_url( __FILE__ ) . 'ajax-handler.php', // @THIS WILL HOLD YOUR AJAX URL :) To retrieve this inside your script.js simply call: pluginslug_scriptname_i18n.ajax_url
'wp_root' => ABSPATH // @THIS WILL HOLD THE ROOT PATH :) To retrieve this inside your script.js simply call: pluginslug_scriptname_i18n.wp_root
)
);
wp_enqueue_script( $handle );
Your script.js
could look like this:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 ){
if (this.status == 200) {
// Success:
}
// Complete:
}
};
xhttp.onerror = function () {
console.log(this);
console.log("** An error occurred during the transaction");
};
xhttp.open("POST", pluginslug_scriptname_i18n.ajax_url, true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
var params = JSON.stringify({
first_name: 'Johny',
wp_root: pluginslug_scriptname_i18n.wp_root
});
xhttp.send(params);
Now inside your ajax-handler.php
you can retrieve the wp_content_dir
and load your wp-load.php
like so:
// Set proper content type
header('Content-Type: text/html');
// Disable caching
header('Cache-Control: no-cache');
header('Pragma: no-cache');
// Get's the payload
$request_body = json_decode( file_get_contents('php://input'), true );
// Set this to true to just load the basics!
// Only set this to true if you know what you are doing
// Lookup SHORTINIT inside wp-settings.php for more details
define( 'SHORTINIT', false );
// Include wp-load.php
require_once( $request_body['wp_root'] . 'wp-load.php' );
die();
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 called SHORTINIT
(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. The SHORTINIT
is defined inside wp-settings.php
(just open up the file and look for SHORTINIT
. 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 the wp-load.php
changes frequently with new releases of WP versions, so if you use SHORTINIT
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 set SHORTINIT
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.
echo get_wp_abspath('wp-load.php') . PHP_EOL;
function get_wp_abspath($filename)
{
$_DIR = __DIR__; // location of this script
$_FILE = $_DIR . '/' . $filename; // fullpath/file
while(!file_exists($_FILE)) // if not found
{
$_DIR = dirname($_DIR); // up one step (i.e. ../)
$_FILE = $_DIR . '/' . $filename; // new fullpath/file
if($_DIR == '/') return FALSE; // if we hit the base path, fail
}
return $_FILE; // if file found, return fullpath/file
}
Wouldn't
include $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php';
work in most situations?
WPINC
and ABSPATH
aren't defined at this point (if they are, wp-load.php
has probably already been included, as someone pointed out.
You can use below code to use wp-load.php to include wp-load from any location
require_once( trailingslashit( ABSPATH ) .'wp-load.php' );
本文标签: plugin developmentHow to include wploadphp from any location
版权声明:本文标题:plugin development - How to include wp-load.php from any location? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736303957a1932064.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论