rest_output_link_wp_head()


WordPressの関数rest_output_link_wp_head()は、WordPressサイトのHTMLのセクションにREST APIエンドポイントのリンクを追加するために使用される。

構文

rest_output_link_wp_head();

例1: REST APIリンクの出力

このコードを使用すると、WordPressのデフォルトのREST APIリンクが内に追加される。

add_action('wp_head','rest_output_link_wp_head');

例2: REST APIリンクの削除

REST APIのリンクが不要な場合は、次のコードで削除できる。

remove_action('wp_head','rest_output_link_wp_head');

例3: 管理画面では削除

フロントエンドではREST APIリンクを保持しつつ、管理画面では削除する。

if(is_admin()){remove_action('wp_head','rest_output_link_wp_head');}

例4: 条件付きでREST APIリンクを追加

特定のページでのみREST APIリンクを追加する。

if(is_single()){add_action('wp_head','rest_output_link_wp_head');}

例5: フィルターフックを使用して変更

wp_headではなく、別のフックで出力する。

add_action('wp_footer','rest_output_link_wp_head');

例6: カスタム条件で削除

特定の投稿タイプのみREST APIリンクを削除する。

if(is_singular('custom_post_type')){remove_action('wp_head','rest_output_link_wp_head');}

例7: カスタム関数で処理

別の関数を作成し、条件に応じてリンクを管理。

function custom_rest_api_link(){if(!is_page('about')){rest_output_link_wp_head();}}
add_action('wp_head','custom_rest_api_link');

例8: すべてのREST API関連リンクを無効化

REST API関連のヘッダーリンクをすべて削除する方法。

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

注意事項

  • REST APIリンクを削除すると、外部アプリケーションのアクセスに影響を与える可能性がある。
  • テーマやプラグインによってはこの関数が必要な場合があるため、削除前に確認が必要。

関連機能: