WordPress создает технические страницы /wp-json/, которые могут индексироваться поисковиками, и в индекс попадают мусорные страницы.

Отключим REST API только для посетителей, которые не вошли в WordPress. 

Можете скачать самый простой и быстрый плагин Disable WP REST API или добавить следующий код в functions.php.

Начнем:

  1. Уберем из http заголовка ссылку из REST API: https://site.ru/wp-json/; rel=»https://api.w.org/»
remove_action('template_redirect', 'rest_output_link_header', 11);

2. Уберем ссылку из <head>: <link rel=’https://api.w.org/’ href=’https://example.com/wp-json/’ />

remove_action('wp_head', 'rest_output_link_wp_head', 10);
remove_action('xmlrpc_rsd_apis', 'rest_output_rsd');

3. Отключим REST API для неавторизованных полльзователей

/*
	Disable REST API
*/
if (version_compare(get_bloginfo('version'), '4.7', '>=')) {
	add_filter('rest_authentication_errors', 'disable_wp_rest_api');
} else {
	disable_wp_rest_api_legacy();
}

function disable_wp_rest_api($access) {
	if (!is_user_logged_in() && !disable_wp_rest_api_allow_access()) {
		$message = apply_filters('disable_wp_rest_api_error', __('REST API restricted to authenticated users.', 'disable-wp-rest-api'));
		return new WP_Error('rest_login_required', $message, array('status' => rest_authorization_required_code()));
	}
	return $access;
}

function disable_wp_rest_api_allow_access() {
	$post_var = apply_filters('disable_wp_rest_api_post_var', false);
	if (!empty($post_var)) {
		if (isset($_POST[$post_var]) && !empty($_POST[$post_var])) return true;
	}
	return false;
}

function disable_wp_rest_api_legacy() {
	
    // REST API 1.x
    add_filter('json_enabled', '__return_false');
    add_filter('json_jsonp_enabled', '__return_false');
	
    // REST API 2.x
    add_filter('rest_enabled', '__return_false');
    add_filter('rest_jsonp_enabled', '__return_false');
	
}

function disable_wp_rest_api_plugin_links($links, $file) {
	if ($file === plugin_basename(__FILE__)) {
		$home_href  = 'https://perishablepress.com/disable-wp-rest-api/';
		$home_title = esc_attr__('Plugin Homepage', 'disable-wp-rest-api');
		$home_text  = esc_html__('Homepage', 'disable-wp-rest-api');
		
		$links[] = '<a target="_blank" rel="noopener noreferrer" href="'. $home_href .'" title="'. $home_title .'">'. $home_text .'</a>';
		
		$rate_href  = 'https://wordpress.org/support/plugin/disable-wp-rest-api/reviews/?rate=5#new-post';
		$rate_title = esc_attr__('Please give a 5-star rating! A huge THANK YOU for your support!', 'disable-wp-rest-api');
		$rate_text  = esc_html__('Rate this plugin', 'disable-wp-rest-api') .'&nbsp;&raquo;';
		
		$links[] = '<a target="_blank" rel="noopener noreferrer" href="'. $rate_href .'" title="'. $rate_title .'">'. $rate_text .'</a>';
		}
	return $links;
}
add_filter('plugin_row_meta', 'disable_wp_rest_api_plugin_links', 10, 2);

Как разрешить доступ для contact form 7?

Как выяснилось, плагину Contact Form 7 требуется доступ к REST API, чтобы контактная форма могла отправлять электронные письма. Для этого можете добавить код в functions.php или скачать отдельный плагин тут.

function disable_wp_rest_api_post_var($var) { return '_wpcf7'; }
add_filter('disable_wp_rest_api_post_var', 'disable_wp_rest_api_post_var');

@seomur

Помощь и консультации по wordpress, woocommerce и ускорению сайта - https://t.me/seomur

0 0 голоса
Рейтинг статьи
Подписаться
Уведомить о
0 комментариев
Межтекстовые Отзывы
Посмотреть все комментарии
0
Оставьте комментарий! Напишите, что думаете по поводу статьи.x