Security PHP Beginner

Block XML-RPC Completely

Turn off XML-RPC, hide the pingback header and refuse direct requests to xmlrpc.php, a favourite target for brute-force attacks.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 17 lines
// 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 );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

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.

Don't use with Jetpack, WordPress mobile app. Check the notes below before adding it.

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.

How it works

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.

Check before you use it

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.

Block it at the server too

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.

How to test it

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

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only