概要
wp_robots_noindex_search
フィルタは、検索リクエスト時のrobotsメタ要素にnoindexを適用するために使用されます。このフィルタを使用することにより、特定の条件下で検索結果をインデックスさせないように設定することができます。これにより、SEOを最適化し、検索エンジンが不要なページをインデックスしないように制御することができます。
wp_robots_noindex_search
フィルタは、以下のような機能を実装する際によく使われます:
- 検索結果ページのnoindex設定
- カスタム検索ページのオプトアウト
- プライバシー設定を強化するためのフィルタリング
- 特定のユーザーロールに対する異なる検索エクスペリエンスの提供
- 検索エンジンのクローラによる不必要なトラフィックの削減
- ページランクの向上を目指すサイト運営
- コンテンツの選択的公開
- 重複コンテンツに対する対策
構文
add_filter('wp_robots_noindex_search', 'custom_noindex_search');
function custom_noindex_search($noindex) {
return true; // すべての検索結果ページにnoindexを適用
}
パラメータ
- $noindex: 現在のnoindex設定を示す boolean 値。
戻り値
- boolean: noindexを適用するかどうかの設定を返します。
関連する関数
wp_robots_noindex_searchフィルタに関連する関数
使用可能なバージョン
このフィルタは、WordPress 4.4以降で使用可能です。
コアファイルのパス
wp-includes/class-wp-robots.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: 検索結果でのnoindexの適用
add_filter('wp_robots_noindex_search', '__return_true');
このコードは、すべての検索結果ページにnoindexを適用します。
サンプル2: 管理画面からの設定変更
add_filter('wp_robots_noindex_search', 'conditional_noindex');
function conditional_noindex($noindex) {
if (is_search() && !current_user_can('administrator')) {
return true;
}
return $noindex;
}
このコードは、管理者以外のユーザーが検索結果にアクセスする場合にnoindexを適用します。
サンプル3: 特定の条件でnoindexを制御
add_filter('wp_robots_noindex_search', 'specific_noindex_control');
function specific_noindex_control($noindex) {
if (is_search() && isset($_GET['exclude'])) {
return true; // excludeパラメータが指定されている場合にnoindexを適用
}
return $noindex;
}
このコードは、exclude
パラメータが指定されている場合のみnoindexを適用します。
サンプル4: 代替canonicalを設定
add_filter('wp_robots_noindex_search', 'custom_canonical');
function custom_canonical($noindex) {
if (is_search()) {
$canonical_url = get_home_url(); // ホームページをcanonical URLとして設定
printf('<link rel="canonical" href="%s" />', esc_url($canonical_url));
return true;
}
return $noindex;
}
このコードは、検索結果ページにnoindexを適用しつつ、ホームページをcanonical URLとして指定します。
サンプル5: 投稿の種類に基づくnoindex設定
add_filter('wp_robots_noindex_search', 'post_type_noindex');
function post_type_noindex($noindex) {
if (is_search() && get_query_var('post_type') === 'custom_post_type') {
return true; // 特定の投稿タイプの場合にnoindexを適用
}
return $noindex;
}
このコードは、特定のカスタム投稿タイプの検索結果にnoindexを適用します。