概要
get_allowed_http_origins
フィルタは、WordPressが許可するHTTPオリジンのリストを取得するために使用されます。このフィルタは、主に以下のような機能を実装する際に頻繁に利用されます。
- CORS(Cross-Origin Resource Sharing)対応
- APIのセキュリティ設定
- サードパーティのサービスとの統合
- ウェブアプリケーションのオリジン制御
- プラグインやテーマによるカスタマイズ
- AJAXリクエストの制限
- フロントエンドとバックエンドの相互作用
- クライアントサイドアプリケーションとの連携
構文
add_filter('allowed_http_origins', 'custom_allowed_http_origins');
パラメータ
$origins
(array): 許可されたオリジンのリスト。
戻り値
- (array): カスタマイズされた許可されるオリジンのリスト。
関連する関数
使用可能なバージョン
- WordPress 4.7.0以降
コアファイルのパス
wp-includes/rest-api.php
サンプルコード
サンプルコード1
add_filter('allowed_http_origins', function($origins) {
$origins[] = 'https://example.com';
return $origins;
});
このコードは、https://example.com
を許可されたオリジンに追加します。
サンプルコード2
add_filter('allowed_http_origins', function($origins) {
return array_merge($origins, ['https://another-domain.com']);
});
このコードは、another-domain.com
を許可されたオリジンのリストに追加しています。
サンプルコード3
add_filter('allowed_http_origins', function($origins) {
if (is_user_logged_in()) {
$origins[] = 'https://loggedin-user.com';
}
return $origins;
});
このコードは、ユーザーがログインしている場合にのみ特定のオリジンを追加します。
サンプルコード4
add_filter('allowed_http_origins', function($origins) {
return [];
});
このコードは、すべてのオリジンを無効にします。
サンプルコード5
add_filter('allowed_http_origins', function($origins) {
if (defined('MY_CUSTOM_ENV') && MY_CUSTOM_ENV === 'development') {
$origins[] = 'http://localhost';
}
return $origins;
});
このコードは、開発環境(例:ローカル環境)でのみ特定のオリジンを追加します。
この関数のアクションでの使用可能性
アクション | 使用例 |
---|---|
mu_plugin_loaded | |
registered_post_type | |
plugins_loaded | 〇 |
wp_roles_init | |
setup_theme | |
after_setup_theme | |
set_current_user | |
init | |
register_sidebar | |
wp_loaded | |
send_headers | |
parse_query | |
pre_get_posts | |
wp | |
template_redirect | |
get_header | |
wp_head |
このフィルタは現在のWordPressバージョンで非推奨または削除されていません。