Skip to content

Instantly share code, notes, and snippets.

@DrizzlyOwl
Last active March 10, 2017 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DrizzlyOwl/5144f6e7b5bd7269ccd9ba592c9df059 to your computer and use it in GitHub Desktop.
Save DrizzlyOwl/5144f6e7b5bd7269ccd9ba592c9df059 to your computer and use it in GitHub Desktop.
WP REST API Disable
<?php
/**
* $. Remove & disable JSON API
******************************************************************************/
function wpst_remove_json_api() {
/**
* Remove API scripts from header/footer
*/
remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );
remove_action( 'wp_head', 'wp_oembed_add_discovery_links', 10 );
remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );
remove_action( 'wp_head', 'wp_oembed_add_host_js' );
remove_action( 'rest_api_init', 'wp_oembed_register_route' );
remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10 );
add_filter( 'embed_oembed_discover', '__return_false' );
/**
* Disable API from working if possible
*/
add_filter( 'json_enabled', '__return_false' );
add_filter( 'json_jsonp_enabled', '__return_false' );
add_filter( 'rest_jsonp_enabled', '__return_false' );
/**
* Remove oembed and REST pingbacks
*/
remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );
remove_action( 'wp_head', 'wp_oembed_add_discovery_links', 10 );
remove_action( 'template_redirect', 'rest_output_link_header', 11 );
}
add_action( 'init', 'wpst_remove_json_api' );
/**
* Force a 403 Forbidden error
*/
function wpst_disable_json_api_access( $result ) {
if ( ! empty( $result ) ) {
return $result;
}
if ( ! is_user_logged_in() ) {
return new WP_Error( 'rest_not_logged_in', 'You are not currently logged in.', ['status' => 401] );
}
}
add_action( 'rest_authentication_errors', 'wpst_disable_json_api_access' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment