0

When site_url() and home_url() are different, how a request should be directed to admin-ajax.php (both in front-end and admin)? Using A. admin_url('wp-admin/admin-ajax.php') or B. home_url('wp-admin/admin-ajax.php')

Usually method A is used. However for some websites this request gives error 404 in front-end. Method B does not work when wordpress is located in some sub-directory with respect to home_url().

Is there any method that works in all the cases when site_url() and home_url() are different?

See also here for the differences between site_url() and home_url(): What's the difference between home_url() and site_url()

1 Answer 1

1

admin_url() should be used.

However for some websites this request gives error 404 in front-end

admin_url('admin-ajax.php') should point to an actual file (admin-ajax.php), as such WordPress won't pass it through index.php, and so it shouldn't lead to the theme's 404 page. (Unless the file has been deleted).

... unless you have a .htaccess file which is redirecting the user away from wp-admin (for example) (a recent client of mine had been doing this to whitelist IP addresses that could reach wp-admin).

2
  • Shouldn't you ommit wp-admin/ in admin_url function? Commented Oct 20, 2016 at 21:01
  • @DmitriyDemir - yes, you're right. I've now corrected it. Commented Oct 21, 2016 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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