LoginSignup
25
25

More than 5 years have passed since last update.

WordPress 4.4 で追加された REST API の停止やエンドポイントの変更など

Last updated at Posted at 2015-12-13

WordPress 4.4 で新しく追加された REST API の停止やエンドポイントの変更方法などを纏めてみた。注意点として、REST API を停止させた場合、他の WordPress サイトに、あなたの記事の URL が掲載された時、あなたの記事が他の WordPress サイトに埋め込まれなくなります(oEmbed)。

REST API のエンドポイントを変更する

デフォルトのエンドポイント wp-json を my-json に変更する例です。

add_filter( 'rest_url_prefix', function () {
    return 'my-json';
} );

要パーマリンクの更新。

REST API の Root index データを変更する

ttp://example.com/wp-json/ にアクセスした際に表示されるデータを変更する。以下に、URL 情報を書き換える例を示します。

add_filter( 'rest_index', function ( $response ) {
    $response->data['url'] = get_option( 'home' );

    return $response;
} );

$response->data['url'] のデフォルト値は、get_option( 'siteurl' ) になっている。

HTML の Head 内に oEmbed スクリプト (JS) を出力させない

remove_action( 'wp_head', 'wp_oembed_add_host_js' );

Really Simple Discovery (RSD) に REST API のエンドポイントを出力させない

remove_action( 'xmlrpc_rsd_apis', 'rest_output_rsd' );

HTML の Head 内に REST API のエンドポイントを出力させない

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

リクエストヘッダーに REST API のエンドポイントを出力させない

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

REST API のリクエストを停止させる

リスエストがあった場合 404 が返るようになる。JSONP の出力も停止します。

add_filter( 'rest_enabled', '__return_false' );

JSONP の出力を停止させる

add_filter( 'rest_jsonp_enabled', '__return_false' );

REST API を停止して 404 ページを表示させる

使わないのであれば、404 にしてしまおうというパターンです。

/**
 * Disable rest api.
 */
remove_action( 'wp_head', 'wp_oembed_add_host_js' );
remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );
remove_action( 'wp_head', 'rest_output_link_wp_head' );
remove_action( 'xmlrpc_rsd_apis', 'rest_output_rsd' );
remove_action( 'template_redirect', 'rest_output_link_header', 11 );
add_filter( 'rest_enabled', '__return_false' );
add_action( 'rest_api_init', function () {
    global $wp_query;

    $template = ! empty( get_404_template() ) ? get_404_template() : get_index_template();

    $wp_query->set_404();
    status_header( 404 );
    nocache_headers();

    require_once( $template );

    exit;
}, - 1 );
}

こちらも合わせてどうぞ !

25
25
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25
25