How can I rename the WordPress AJAX URL? [duplicate]

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:

RewriteRule ^/?ajax/?$ /wp-admin/admin-ajax.php?&%{QUERY_STRING} [L,QSA]

So now, all the AJAX requests should be visible as /ajax/ and not as wp-admin/admin-ajax.php

Leave a Comment