0

I currently work on localhost and when I want to test ajax requests to my web server, the function get_current_user_id() always returns 0. When I make the ajax request from my website however, it works. Opening the file in the browser also returns the current wp_user id. The file I make the requests to:

<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php';
echo get_current_user_id();

I have cors in my .htaccess enabled, other WordPress functions work and I get no warnings or errors in my console.

My .htaccess:

<IfModule mod_rewrite.c>
Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, x-requested-with, 
content-type"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, 
OPTIONS"
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
ErrorDocument 401 /index.php?status=404
ErrorDocument 403 /index.php?status=404
ErrorDocument 404 /index.php?status=404
ErrorDocument 500 /index.php?status=404
</IfModule>

Thanks for any help in advance!

Edit: Due to the answer from "Tim" I tried the following, but it didn't work.

In my functions.php:

function example_ajax_request()
{
    echo get_current_user_id();
    die();
}
add_action('wp_ajax_example_ajax_request', 'example_ajax_request');
add_action('wp_ajax_nopriv_example_ajax_request', 'example_ajax_request');

Javascript on localhost:

var ajax = new XMLHttpRequest();
var formData = new FormData();
formData.append("action", "example_ajax_request");
ajax.open(
  "POST",
  "https://my-website.com/wp-admin/admin-ajax.php"
);
ajax.onload = function() {
  console.log(ajax.responseText);
};
ajax.send(formData);

---> returns 0 in console as user is not logged in

Update - Found the solution:

Problem was that javascript didn't send cookies automatically cross-domain. I had to set

var ajax = new XMLHttpRequest();
ajax.withCredentials = true;

After that, I had to add

Header add Access-Control-Allow-Credentials true

to my .htaccess, so cross-domain cookie transfer is allowed.

-1

Don't write your own bootstrapper, use Ajax hooks via wp-admin/admin-ajax.php

Documentation: https://codex.wordpress.org/AJAX_in_Plugins

Then you can be sure WordPress has included all the functions you need.


Update:

I missed the bit about posting from local machine to remote server. If your JavaScript is running on another domain, it won't be sending back your web server's login cookies. CORS cannot help you with that.

  • Would the down-voter care to elaborate? – Tim Nov 19 '18 at 11:02
  • I didn't do it, I updated my question, but didn't work. – Tim von Känel Nov 19 '18 at 11:15
  • Although you should use hooks, I realise this is not the solution in your case. Your problem is cross-domain authentication. – Tim Nov 19 '18 at 13:13
  • Ye, i solved my problem, it was indeed that js didn't send cookies cross-domain automatically. – Tim von Känel Nov 20 '18 at 2:54

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.