Skip to content

Instantly share code, notes, and snippets.

@bueltge
Last active April 30, 2020 04:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bueltge/2a084907ae029ce5fbba9098627fd5d3 to your computer and use it in GitHub Desktop.
Save bueltge/2a084907ae029ce5fbba9098627fd5d3 to your computer and use it in GitHub Desktop.
Disable WordPress REST API for users, there have not enough rights
<?php # -*- coding: utf-8 -*-
declare(strict_types=1);
/**
* Plugin Name: Disable REST API
*/
// Completely disable wp-json access.
add_filter(
'rest_authentication_errors',
function () {
if (! current_user_can('edit_posts')) {
return new WP_Error('rest_cannot_access', 'Bye', ['status' => 403]);
}
}
);
// Remove actions added by wp-includes/default-filters.php.
remove_action('wp_head', 'rest_output_link_wp_head');
remove_action('wp_head', 'wp_oembed_add_discovery_links');
remove_action('template_redirect', 'rest_output_link_header');
// Disable also the XMLRPC endpoint, not necessary for the REST API.
add_filter('xmlrpc_enabled', '__return_false');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment