プラグインCustom Post Type UIのcptui_reserved_taxonomiesフィルタの使用方法・解説

概要

cptui_reserved_taxonomies フィルタは、WordPressのCustom Post Type UIプラグインで使用されるフィルタです。Custom Post Type UIは、ユーザーが簡単にカスタム投稿タイプとタクソノミーを作成できるようにするためのプラグインです。このフィルタは、デフォルトで予約されているタクソノミーを変更したり、追加したりするのに役立ちます。

このフィルタは、以下の目的によく利用されます:
1. カスタムタクソノミーの追加
2. デフォルトのタクソノミーを変更
3. 特定のタクソノミーの非表示化
4. タクソノミーの属性の追加
5. 一括操作時のタクソノミーの管理
6. 関連コンテンツのフィルタリング

フィルタの概要

  • フィルタ名cptui_reserved_taxonomies
  • 構文add_filter( 'cptui_reserved_taxonomies', 'your_callback_function' );
  • パラメータ:配列形式で渡されるタクソノミーの一覧
  • 戻り値:変更後のタクソノミーの配列
  • 使用可能なプラグインバージョン:Custom Post Type UIバージョン1.9.0以降
  • 使用可能なWordPressバージョン:WordPressバージョン4.0以降

サンプルコード

サンプルコード 1

add_filter( 'cptui_reserved_taxonomies', function( $taxonomies ) {
    $taxonomies[] = 'custom_taxonomy';
    return $taxonomies;
});

このコードは、’custom_taxonomy’という名前のカスタムタクソノミーを追加します。

サンプルコード 2

add_filter( 'cptui_reserved_taxonomies', function( $taxonomies ) {
    unset( $taxonomies['category'] ); // 'category'タクソノミーを削除
    return $taxonomies;
});

このコードは、デフォルトの’category’タクソノミーを予約されたタクソノミーのリストから削除します。

サンプルコード 3

add_filter( 'cptui_reserved_taxonomies', function( $taxonomies ) {
    $taxonomies['my_taxonomy'] = [
        'labels' => [
            'name' => 'My Taxonomy',
            'singular_name' => 'My Taxonomy Item',
        ],
        'hierarchical' => true,
    ];
    return $taxonomies;
});

このコードは、’my_taxonomy’という新しいタクソノミーを作成し、そのラベルと階層構造を設定します。

サンプルコード 4

add_filter( 'cptui_reserved_taxonomies', function( $taxonomies ) {
    return array_merge( $taxonomies, ['new_taxonomy', 'another_taxonomy'] );
});

このコードは、’new_taxonomy’と’another_taxonomy’という2つのタクソノミーを追加します。

サンプルコード 5

add_filter( 'cptui_reserved_taxonomies', function( $taxonomies ) {
    return array_diff( $taxonomies, ['old_taxonomy'] ); // 'old_taxonomy'を削除
});

このコードは、’old_taxonomy’というタクソノミーを予約されたタクソノミーのリストから削除します。

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

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

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


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