How can I rename the WordPress AJAX URL? [duplicate]
IT Nursery
May 20, 2022
0
I’m currently looking for a way to change / hide the default WordPress admin-ajax.php URL. I want to do this to remove the admin from it to prevent misunderstandings by my customers when I use the AJAX URL to read for example a file from my server. In this case, the URL has the admin prefix which is a bit confusing.
So in result the AJAX URl should looks like this: ajax.php
I’ve searched a bit but I can’t find any helpful informations. Because of this I can’t show you any related code. But when I got it, I’ll update my question to help other people.
Maybe there is someone who did this before and can give me some helpful tips so that I can implement this asap? This would be very awesome!
2 Answers 2
It’s not as hard to solve as one might think… If only all plugins/themes respect best practices.
If they do, then all links to admin-ajax.php are generated with admin_url function. And this function has a hook inside, so we can modify the url it returns:
// This will change the url for admin-ajax.php to /ajax/
function modify_adminy_url_for_ajax( $url, $path, $blog_id ) {
if ( 'admin-ajax.php' == $path ) {
$url = site_url('/ajax/');
}
return $url;
}
add_filter( 'admin_url', 'modify_adminy_url_for_ajax', 10, 3 );
So now we have to teach WordPress to process such requests. And we can use .htaccess to do so. This line should do the trick: