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()

progmastery
  • 143
  • 6

1 Answers1

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).

Stephen Harris
  • 32,662
  • 6
  • 84
  • 118