Safely disable WP REST API

后端 未结 5 1749
花落未央
花落未央 2021-01-30 13:37

I am considering to improve security of my Wordpress website, and in doing so have come across WP REST API being enabled by default (since WP 4.4 if I\'m not mistaken).

5条回答
  •  猫巷女王i
    2021-01-30 14:05

    You can disable it for requests other than localhost:

    function restrict_rest_api_to_localhost() {
        $whitelist = [ '127.0.0.1', "::1" ];
    
        if( ! in_array($_SERVER['REMOTE_ADDR'], $whitelist ) ){
            die( 'REST API is disabled.' );
        }
    }
    add_action( 'rest_api_init', 'restrict_rest_api_to_localhost', 0 );
    

提交回复
热议问题