Home / Admin / _MK – SECURITY – Disable XML-RPC-API
Duplicate Snippet

Embed Snippet on Your Site

_MK – SECURITY – Disable XML-RPC-API

<10
Code Preview
php
<?php
// Disable XML-RPC
add_filter( 'xmlrpc_enabled', '__return_false' );
// Block access to xmlrpc.php via .htaccess equivalent (Apache fallback)
add_action( 'init', function () {
    if (strpos($_SERVER['REQUEST_URI'], 'xmlrpc.php') !== false) {
        wp_die( 'Access denied.' );
    }
});
// Remove pingback header
add_filter( 'wp_headers', function( $headers ) {
    unset( $headers['X-Pingback'] );
    return $headers;
});
// Remove pingback link from the <head>
remove_action( 'wp_head', 'rsd_link' );
remove_action( 'wp_head', 'wp_generator' );
remove_action( 'wp_head', 'wlwmanifest_link' );
remove_action( 'wp_head', 'wp_shortlink_wp_head' );
remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );
remove_action( 'template_redirect', 'rest_output_link_header', 11, 0 );
// Disable trackbacks and pingbacks
add_filter( 'xmlrpc_methods', function( $methods ) {
    unset( $methods['pingback.ping'] );
    unset( $methods['pingback.extensions.getPingbacks'] );
    unset( $methods['trackback.ping'] );
    return $methods;
});
// Disable REST API for non-logged-in users
add_filter( 'rest_authentication_errors', function( $result ) {
    if ( ! is_user_logged_in() ) {
        return new WP_Error( 'rest_disabled', 'REST API restricted.', array( 'status' => 403 ) );
    }
    return $result;
});
// Disable WordPress file editor
// define( 'DISALLOW_FILE_EDIT', true );
// Optional: Hide WordPress version
add_filter('the_generator', '__return_empty_string');

Comments

Add a Comment