Turn off XML-RPC, hide the pingback header and refuse direct requests to xmlrpc.php, a favourite target for brute-force attacks.
// Turn off the XML-RPC API.
add_filter( 'xmlrpc_enabled', '__return_false' );
// Stop advertising it in headers and the page head.
add_filter( 'wp_headers', function ( array $headers ): array {
unset( $headers['X-Pingback'] );
return $headers;
} );
remove_action( 'wp_head', 'rsd_link' );
// Refuse direct requests to xmlrpc.php.
add_action( 'init', function (): void {
if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) {
status_header( 403 );
exit( 'XML-RPC is disabled on this site.' );
}
}, 0 );
Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.
XML-RPC is an old remote-publishing API. Most sites no longer use it, but it still answers requests, and attackers use its system.multicall method to try hundreds of passwords in a single request. This snippet switches it off and stops WordPress from advertising it.
The xmlrpc_enabled filter turns off every XML-RPC method that needs a login. The wp_headers filter removes the X-Pingback header, and removing rsd_link drops the discovery tag from your page head.
The last part blocks the endpoint itself. When WordPress loads through xmlrpc.php it defines XMLRPC_REQUEST, so checking that constant early on init catches every request to the file and returns a 403 before any more work is done. This is more reliable than inspecting the script file name, which can differ between hosts.
Jetpack and the official WordPress mobile apps still connect over XML-RPC. If you rely on either, don’t use this snippet, or use a security plugin that allows only those services.
This snippet still lets WordPress load before refusing the request. On busy sites, also block the file at the web server or your CDN, for example with a Cloudflare WAF rule matching the path /xmlrpc.php, so the requests never reach PHP at all.
Visit yoursite.com/xmlrpc.php. You should see “XML-RPC is disabled on this site.” with a 403 status instead of the usual “XML-RPC server accepts POST requests only.”