プラグインThe Events Calendarのtec_events_noindexフィルタの使用方法・解説

概要

tec_events_noindexフィルタは、The Events Calendarプラグインが提供する特定のページを検索エンジンのインデックスから除外するために使用されるフックです。このフィルタは、SEO対策や特定のコンテンツの非公開性を保つために役立ちます。以下のような機能に利用されることが一般的です:

  1. イベントアーカイブページの検索エンジンインデックスからの除外
  2. スペシャルイベントやプライベートイベントページの非インデックス化
  3. サイトのコンテンツ管理とSEOの最適化
  4. 非公開なカスタムイベントタイプの処理
  5. フィルタを特定のユーザーやロールに基づいて適用
  6. SEOプラグインとの連携による柔軟な設定

このフィルタは、The Events Calendarプラグインのバージョン5.0以降、WordPressのバージョン5.0以降で使用可能です。

構文

add_filter('tec_events_noindex', 'your_function_name', 10, 3);

パラメータ

  • $noindex (bool): デフォルトではfalse。trueに設定されると、インデックスされないことを示します。
  • $post_type (string): 現在の投稿タイプを示します。
  • $event (WP_Post): 現在のイベントのWP_Postオブジェクト。

戻り値

  • (bool): インデックスを除外するかどうかを示す真偽値。

この関数のアクションでの使用可能性

アクション 使用可能性
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

add_filter('tec_events_noindex', function ($noindex, $post_type) {
    if ($post_type === 'tribe_events') {
        return true; // イベントアーカイブをnoindexに設定
    }
    return $noindex;
});

説明: これは、イベントの投稿タイプが’tribe_events’である場合に、インデックスを除外する設定を行うサンプルコードです。特にイベントアーカイブページをnoindexに設定します。
引用元URL: https://theeventscalendar.com/

サンプルコード2

add_filter('tec_events_noindex', function ($noindex, $post_type, $event) {
    if ($event->post_status === 'private') {
        return true; // プライベートイベントをnoindexに設定
    }
    return $noindex;
});

説明: プライベートイベントの投稿ステータスが設定されている場合にそのイベントをnoindexにします。
引用元URL: https://theeventscalendar.com/

サンプルコード3

add_filter('tec_events_noindex', function ($noindex) {
    if (is_user_logged_in()) {
        return false; // ログインユーザーにはインデックスを許可
    }
    return $noindex;
});

説明: ログインしているユーザーには、イベントページのインデックスを許可する設定を行ったサンプルコードです。
引用元URL: https://theeventscalendar.com/

サンプルコード4

add_filter('tec_events_noindex', function ($noindex) {
    $current_date = current_time('Y-m-d');
    return ($current_date > '2023-12-31'); // 特定の期間後にnoindexを適用
});

説明: 現在の日付が2023年末を超えている場合、全てのイベントをnoindexにする設定です。
引用元URL: https://theeventscalendar.com/

サンプルコード5

add_filter('tec_events_noindex', function ($noindex) {
    $event_categories = get_the_terms(get_the_ID(), 'tribe_events_cat');
    if ($event_categories && in_array('unlisted', wp_list_pluck($event_categories, 'slug'))) {
        return true; // 'unlisted'カテゴリが含まれるイベントはnoindex
    }
    return $noindex;
});

説明: ‘unlisted’というカテゴリが付けられたイベントのみをnoindexにする設定を行っています。
引用元URL: https://theeventscalendar.com/

この関数について質問する


上の計算式の答えを入力してください