最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

themes - Remove JSON API links in header html

programmeradmin3浏览0评论

Does anyone know how to remove the WordPress JSON API links in the header tag?

<head>
...
<link rel='/' href='/' />
<link rel="alternate" type="application/json+oembed" href=".0/embed?url=..." />
<link rel="alternate" type="text/xml+oembed" href=".0/embed?url=..." />
</head>

I'd like to avoid using a plugin. If possible, is there a way to remove them with the remove_action function?

remove_action( 'wp_head', 'rsd_link' );

Does anyone know how to remove the WordPress JSON API links in the header tag?

<head>
...
<link rel='https://api.w/' href='http://example/wp-json/' />
<link rel="alternate" type="application/json+oembed" href="http://example/wp-json/oembed/1.0/embed?url=..." />
<link rel="alternate" type="text/xml+oembed" href="http://example/wp-json/oembed/1.0/embed?url=..." />
</head>

I'd like to avoid using a plugin. If possible, is there a way to remove them with the remove_action function?

remove_action( 'wp_head', 'rsd_link' );
Share Improve this question edited Jan 2, 2016 at 3:48 Jentan Bernardus 5204 silver badges8 bronze badges asked Dec 10, 2015 at 23:30 JordanJordan 5171 gold badge4 silver badges7 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 43

I see in filters.php "add_action( 'wp_head', 'rest_output_link_wp_head', 10, 0 )" Which makes me think this should do the trick to remove rel='https://api.w/'.

remove_action( 'wp_head',      'rest_output_link_wp_head'              );

The rest... * cough * seem to be in default-filters.php

remove_action( 'wp_head',      'wp_oembed_add_discovery_links'         );

To remove the rest_output_link_header

remove_action( 'template_redirect', 'rest_output_link_header', 11 );

Reference

  • wp_oembed_add_discovery_links
  • rest_output_link_wp_head
  • rest_output_link_header

This custom function should help removing all links in the header and footer - you may put it inside the functions.php file of your active theme;

function remove_json_api () {

    // Remove the REST API lines from the HTML Header
    remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );
    remove_action( 'wp_head', 'wp_oembed_add_discovery_links', 10 );

    // Remove the REST API endpoint.
    remove_action( 'rest_api_init', 'wp_oembed_register_route' );

    // Turn off oEmbed auto discovery.
    add_filter( 'embed_oembed_discover', '__return_false' );

    // Don't filter oEmbed results.
    remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10 );

    // Remove oEmbed discovery links.
    remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );

    // Remove oEmbed-specific JavaScript from the front-end and back-end.
    remove_action( 'wp_head', 'wp_oembed_add_host_js' );

   // Remove all embeds rewrite rules.
   add_filter( 'rewrite_rules_array', 'disable_embeds_rewrites' );

}
add_action( 'after_setup_theme', 'remove_json_api' );

And this snippet completely disable the REST API and shows the content below when you visit http://example/wp-json/, were example is the domain name of your website;

{"code":"rest_disabled","message":"The REST API is disabled on this site."}

In order to disable WordPress REST API, use the snippet below;

function disable_json_api () {

  // Filters for WP-API version 1.x
  add_filter( 'json_enabled', '__return_false' );
  add_filter( 'json_jsonp_enabled', '__return_false' );

  // Filters for WP-API version 2.x
  add_filter( 'rest_enabled', '__return_false' );
  add_filter( 'rest_jsonp_enabled', '__return_false' );

}
add_action( 'after_setup_theme', 'disable_json_api' );

The best solution and easy way to disable oEmbed discovery links and wp-embed.min.js is by adding this code snippet in your theme (function.php).

remove_action( 'wp_head', 'wp_oembed_add_discovery_links', 10 ); 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);

Hope this can help someone as the above solutions don't work for me while using the latest version of WordPress.

发布评论

评论列表(0)

  1. 暂无评论