最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp-load.php not working

matteradmin9PV0评论

I'm using require_once(../../../../wp-load.php), which is the correct relative path to the file, but it doesn't work. If I use wp-blog-header.php instead, that works and enables me to use WP functions. Any ideas as to why wp-load.php isn't working?

Thanks, Emir

I'm using require_once(../../../../wp-load.php), which is the correct relative path to the file, but it doesn't work. If I use wp-blog-header.php instead, that works and enables me to use WP functions. Any ideas as to why wp-load.php isn't working?

Thanks, Emir

Share Improve this question asked Feb 11, 2019 at 16:15 MirekoMireko 111 silver badge2 bronze badges 8
  • Imho require_once( ABSPATH . 'wp-load.php' ); should do the trick for you. Read some more about it here – Charles Commented Feb 11, 2019 at 16:27
  • @Charles isn't ABSPATH defined in wp-load.php? – tmdesigned Commented Feb 11, 2019 at 16:35
  • @Emir, why do you not want to use wp-blog-header.php? That does 3 things, wp-load, sets up the WordPress query, and then loads the template. Do you not want to do those latter 2? – tmdesigned Commented Feb 11, 2019 at 16:37
  • Why are you trying to load wp-load.php? If you need an endpoint for JS queries, you can use the REST API, and if you need to run things via command line or cron, WP CLI is the correct way to do that – Tom J Nowell Commented Feb 11, 2019 at 17:25
  • @tmdesigned, I was actualy just responding on the require path he showed, not on the why it isn't working. Because loading a file the way he showed is not really the way it should be in WP imho. – Charles Commented Feb 11, 2019 at 18:06
 |  Show 3 more comments

1 Answer 1

Reset to default 1

You don't need to load wp-load.php, you can build an endpoint to poll with javascript using a REST API endpoint.

In your plugin, or functions.php, we're going to create an example endpoint, it will live at https://yoursite/wp-json/yourname/v1/yourendpoint, it will take in parameters, and reply with a snippet of JSON, just as you would expect from any other REST API.

This API will say "Hello World!".

To create it, put the following in your plugin or functions.php:

function hello() {
    return "Hello World!";
}

Then register it:

add_action( 'rest_api_init', function () {
        register_rest_route( 'yourname/v1', '/yourendpoint/', array(
                'methods' => 'GET',
                'callback' => 'hello'
        ) );
} );

Congratulations! You have a fully functioning REST API endpoint! Try visiting https://yoursite/wp-json/yourname/v1/yourendpoint in your browser and you'll see:

"Hello World!"

If you return an array or an object, they'll show up as the equivalent JSON.

Now, we can call this in our javascript like so:

fetch('/wp-json/yourname/v1/yourendpoint')
  .then(function(response) { return response.json();})
  .then(function(data) {
    alert(data); // outputs Hello World!
    // do stuff
  });

Or if you don't like fetch and prefer jQuery:

jQuery.ajax({
    url: '/wp-json/yourname/v1/yourendpoint'
}).done(function( data ) {
    alert( data );
    // do stuff
});

Make sure you have your permalinks turned on, remember to return data not echo it, and hey presto, you're done! This is just like any other REST API, e.g. the ones twitter, mailchimp and facebook have.

Bonus points:

  • You can pass extra fields when you create your endpoint to tell it who can access it, and which parameters you take, and it'll document it all for you and do all the checking so you don't have to ( and it'll do it correctly, so you don't have to worry about making mistakes )
  • You can use standard HTTP methods e.g. GET to get something, PUT or POST to put/push something, and DELETE to delete something
  • Don't create endpoints to return lists of posts, WordPress already has that at /wp-json/wp/v2/posts! Same with pages etc, you can create them too

But most of all, creating a PHP file in a folder, loading wp-load.php, then calling it directly, is a massive security problem, and breaks easily. Don't do that.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far