0

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

  • Imho require_once( ABSPATH . 'wp-load.php' ); should do the trick for you. Read some more about it here – Charles Feb 11 at 16:27
  • @Charles isn't ABSPATH defined in wp-load.php? – tmdesigned Feb 11 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 Feb 11 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 Feb 11 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 Feb 11 at 18:06
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.com/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.com/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.

  • Thank you @Tom J Nowell very much for your answer. I've just added that and it works! What I'm trying to do now is get the username of person logged in. So have this code function hello() { $current_user = wp_get_current_user(); $employee = $current_user->user_login; return $employee; }, but when I go to the endpoint I get 'false'. Could you please let me know how to fix this? – Mire2030 Feb 14 at 10:23
  • You don't need to, there's already an authenticated endpoint for that at /wp-json/wp/v2/users/me, though it's much easier to just include that data with the page instead of doing an extra request. Check the official docs for the other endpoints already available – Tom J Nowell Feb 14 at 15:11
  • As an aside, of course it'll return false if there's no current user, the code in your comment doesn't handle if a user is not logged in. For that endpoint you'd specify it's for logged in users only when you register your endpoint, but tbh those are new and different questions unrelated to this one. This is a Q&A site, not an open ended investigation, so open new questions! – Tom J Nowell Feb 14 at 15:13
  • Thank you very much @Tom J Nowell. I will look into that. – Mire2030 Feb 15 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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