0

add_filter( 'rest_endpoints', array( $this, 'block_rest_api_endpoints' ) );
  add_filter( 'rest_authentication_errors', array( $this, 'block_rest_api_authentication_errors' ) );
// Disable REST API Endpoints
 public function block_rest_api_endpoints($endpoints){
  if ( isset( $endpoints['/wp/v2/users'] ) ) {
   unset( $endpoints['/wp/v2/users'] );
  }
  if ( isset( $endpoints['/wp/v2/users/(?P<id>[\d]+)'] ) ) {
   unset( $endpoints['/wp/v2/users/(?P<id>[\d]+)'] );
  }
  if ( isset( $endpoints['/wp/v2/media'] ) ) {
   unset( $endpoints['/wp/v2/media'] );
  }
  if ( isset( $endpoints['/wp/v2/media/(?P<id>[\d]+)'] ) ) {
   unset( $endpoints['/wp/v2/media/(?P<id>[\d]+)'] );
  }
  return $endpoints;
 }
 // Disable REST API
 public function block_rest_api_authentication_errors($access) {
  if ( ! empty( $access ) ) {
   return $access;
  }
  if ( is_user_logged_in() ) {
   return $access;
  }
  return new \WP_Error(
   'rest_forbidden',
   __( 'REST API is disabled for public access.' ),
   array( 'status' => 403 )
  );
 }

Jagdish Sarma Asked question 23 hours ago
Add a Comment