概要
get_status_header_desc
関数は、HTTPステータスコードに対する説明文を取得するための関数です。この関数は、特定のHTTPステータスコードに関連付けられた説明文を返し、主にエラーメッセージやレスポンスヘッダーの詳細を表示する際に使用されます。以下は、この関数がよく使われるシナリオの例です。
- カスタムエラーページの表示
- REST APIのレスポンス生成
- テーマのエラーハンドリング
- プラグインのエラーメッセージ表示
- 認証エラーの詳細表示
- サーバーエラーログの記録
- HTTPモニタリングツールとの統合
- ユーザー向けのフィードバック提供
構文
get_status_header_desc( $code );
パラメータ
$code
(int) 必須: HTTPステータスコード。
戻り値
- (string) 指定されたHTTPステータスコードに関連する説明文が返されます。コードが無効な場合は空の文字列が返されます。
関連する関数
使用可能なバージョン
- WordPress 2.1.0以降で利用可能。
コアファイルのパス
wp-includes/functions.php
この関数のアクションでの使用可能性
アクション | 使用可能 |
---|---|
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 |
サンプルコード
サンプル1: カスタムエラーページの表示
function custom_error_page() {
$code = 404; // ステータスコード
status_header($code);
echo get_status_header_desc($code);
}
add_action('template_redirect', 'custom_error_page');
説明: 404エラーページがリダイレクトされる際に、HTTPステータスの説明文を表示します。
サンプル2: REST APIのレスポンス
function my_rest_api_function() {
$response = new WP_REST_Response();
$response->set_status(403);
$response->set_data(get_status_header_desc(403));
return $response;
}
add_action('rest_api_init', 'my_rest_api_function');
説明: REST APIのエンドポイントで、403 Forbiddenの説明を設定します。
サンプル3: プラグインのエラーメッセージ
function plugin_error_message() {
echo get_status_header_desc(500); // 500エラーのメッセージ
}
add_action('wp_footer', 'plugin_error_message');
説明: ウェブページのフッターに500エラーのメッセージを表示します。
サンプル4: ユーザー向けのフィードバック提供
function send_feedback() {
$code = 401;
$message = get_status_header_desc($code);
wp_die($message, $code);
}
add_action('wp_ajax_nopriv_action', 'send_feedback');
説明: 認証されていないユーザーがアクションを実行しようとした際に、401エラーのメッセージを表示します。
サンプル5: HTTPモニタリングツールとの統合
function monitor_http_status() {
$status_code = http_response_code();
$description = get_status_header_desc($status_code);
error_log("HTTP Status: $status_code - $description");
}
add_action('init', 'monitor_http_status');
説明: HTTPレスポンスコードとその説明をエラーログに記録します。
この内容は著作権フリーであり、引用元は特にありませんが、サンプルコードは一般的な使用例に基づいています。