| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Disable WP REST API |
| 4 |
Plugin URI: https://perishablepress.com/disable-wp-rest-api/ |
| 5 |
Description: Disables the WP REST API for visitors not logged into WordPress. |
| 6 |
Tags: rest, rest-api, api, json, disable |
| 7 |
Author: Jeff Starr |
| 8 |
Author URI: https://plugin-planet.com/ |
| 9 |
Donate link: https://monzillamedia.com/donate.html |
| 10 |
Contributors: specialk |
| 11 |
Requires at least: 4.7 |
| 12 |
Tested up to: 7.1 |
| 13 |
Stable tag: 2.6.9 |
| 14 |
Version: 2.6.9 |
| 15 |
Requires PHP: 5.6.20 |
| 16 |
Text Domain: disable-wp-rest-api |
| 17 |
Domain Path: /languages |
| 18 |
License: GPL v2 or later |
| 19 |
License URI: https://www.gnu.org/licenses/gpl-2.0.html |
| 20 |
|
| 21 |
This program is free software; you can redistribute it and/or |
| 22 |
modify it under the terms of the GNU General Public License |
| 23 |
as published by the Free Software Foundation; either version |
| 24 |
2 of the License, or (at your option) any later version. |
| 25 |
|
| 26 |
This program is distributed in the hope that it will be useful, |
| 27 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 28 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 29 |
GNU General Public License for more details. |
| 30 |
|
| 31 |
You should have received a copy of the GNU General Public License |
| 32 |
with this program. If not, visit: https://www.gnu.org/licenses/ |
| 33 |
|
| 34 |
Copyright 2017-2026 Monzilla Media. All rights reserved. |
| 35 |
*/ |
| 36 |
|
| 37 |
if (!defined('ABSPATH')) die(); |
| 38 |
|
| 39 |
/* |
| 40 |
Disable REST API link in HTTP headers |
| 41 |
Link: <https://example.com/wp-json/>; rel="https://api.w.org/" |
| 42 |
*/ |
| 43 |
remove_action('template_redirect', 'rest_output_link_header', 11); |
| 44 |
|
| 45 |
/* |
| 46 |
Disable REST API links in HTML <head> |
| 47 |
<link rel='https://api.w.org/' href='https://example.com/wp-json/' /> |
| 48 |
*/ |
| 49 |
remove_action('wp_head', 'rest_output_link_wp_head', 10); |
| 50 |
remove_action('xmlrpc_rsd_apis', 'rest_output_rsd'); |
| 51 |
|
| 52 |
/* |
| 53 |
Disable REST API |
| 54 |
*/ |
| 55 |
if (version_compare(get_bloginfo('version'), '4.7', '>=')) { |
| 56 |
|
| 57 |
add_filter('rest_authentication_errors', 'disable_wp_rest_api'); |
| 58 |
|
| 59 |
} else { |
| 60 |
|
| 61 |
disable_wp_rest_api_legacy(); |
| 62 |
|
| 63 |
} |
| 64 |
|
| 65 |
function disable_wp_rest_api($access) { |
| 66 |
|
| 67 |
if (!is_user_logged_in() && !disable_wp_rest_api_allow_access()) { |
| 68 |
|
| 69 |
$message = apply_filters('disable_wp_rest_api_error', __('REST API restricted to authenticated users.', 'disable-wp-rest-api')); |
| 70 |
|
| 71 |
return new WP_Error('rest_login_required', $message, array('status' => rest_authorization_required_code())); |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
return $access; |
| 76 |
|
| 77 |
} |
| 78 |
|
| 79 |
function disable_wp_rest_api_allow_access() { |
| 80 |
|
| 81 |
$post_var = apply_filters('disable_wp_rest_api_post_var', false); |
| 82 |
$server_var = apply_filters('disable_wp_rest_api_server_var', false); |
| 83 |
|
| 84 |
if (!empty($post_var)) { |
| 85 |
|
| 86 |
if (is_array($post_var)) { |
| 87 |
|
| 88 |
foreach($post_var as $var) { |
| 89 |
|
| 90 |
if (isset($_POST[$var]) && !empty($_POST[$var])) return true; |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
} else { |
| 95 |
|
| 96 |
if (isset($_POST[$post_var]) && !empty($_POST[$post_var])) return true; |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
if (!empty($server_var)) { |
| 103 |
|
| 104 |
if (is_array($server_var)) { |
| 105 |
|
| 106 |
foreach($server_var as $var) { |
| 107 |
|
| 108 |
if (isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] === $var) return true; |
| 109 |
|
| 110 |
} |
| 111 |
|
| 112 |
} else { |
| 113 |
|
| 114 |
if (isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] === $server_var) return true; |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
return false; |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
function disable_wp_rest_api_legacy() { |
| 125 |
|
| 126 |
// REST API 1.x |
| 127 |
add_filter('json_enabled', '__return_false'); |
| 128 |
add_filter('json_jsonp_enabled', '__return_false'); |
| 129 |
|
| 130 |
// REST API 2.x |
| 131 |
add_filter('rest_enabled', '__return_false'); |
| 132 |
add_filter('rest_jsonp_enabled', '__return_false'); |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
// |
| 137 |
|
| 138 |
function disable_wp_rest_api_plugin_row_meta($links, $file) { |
| 139 |
|
| 140 |
if ($file === plugin_basename(__FILE__)) { |
| 141 |
|
| 142 |
$home_href = 'https://plugin-planet.com/rest-pro-tools/'; |
| 143 |
$home_title = esc_attr__('Get REST Pro Tools', 'disable-wp-rest-api'); |
| 144 |
$home_text = esc_html__('Go Pro', 'disable-wp-rest-api'); |
| 145 |
|
| 146 |
$links[] = '🛠️ <strong><a target="_blank" rel="noopener noreferrer" href="'. $home_href .'" title="'. $home_title .'">'. $home_text .'</a></strong>'; |
| 147 |
|
| 148 |
$rate_href = 'https://wordpress.org/support/plugin/disable-wp-rest-api/reviews/?rate=5#new-post'; |
| 149 |
$rate_title = esc_attr__('Please give a 5-star rating! A huge THANK YOU for your support!', 'disable-wp-rest-api'); |
| 150 |
$rate_text = esc_html__('Rate this plugin', 'disable-wp-rest-api') .' »'; |
| 151 |
|
| 152 |
$links[] = '<a target="_blank" rel="noopener noreferrer" href="'. $rate_href .'" title="'. $rate_title .'">'. $rate_text .'</a>'; |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
return $links; |
| 157 |
|
| 158 |
} |
| 159 |
add_filter('plugin_row_meta', 'disable_wp_rest_api_plugin_row_meta', 10, 2); |
| 160 |
|
| 161 |
function disable_wp_rest_api_plugin_action_links($links, $file) { |
| 162 |
|
| 163 |
if ($file === plugin_basename(__FILE__)) { |
| 164 |
|
| 165 |
$pro_href = 'https://plugin-planet.com/rest-pro-tools/'; |
| 166 |
$pro_title = esc_attr__('Get REST Pro Tools', 'disable-wp-rest-api'); |
| 167 |
$pro_text = esc_html__('Go Pro', 'disable-wp-rest-api'); |
| 168 |
$pro_style = 'padding:2px 4px;font-weight:bold;border:1px solid #00CCCC;border-radius:2px;background-color:#fff;'; |
| 169 |
|
| 170 |
$pro = '<a target="_blank" rel="noopener noreferrer" href="'. $pro_href .'" title="'. $pro_title .'" style="'. $pro_style .'">'. $pro_text .'</a>'; |
| 171 |
|
| 172 |
array_unshift($links, $pro); |
| 173 |
|
| 174 |
} |
| 175 |
|
| 176 |
return $links; |
| 177 |
|
| 178 |
} |
| 179 |
add_filter ('plugin_action_links', 'disable_wp_rest_api_plugin_action_links', 10, 2); |