プラグインCustom Post Type UIのcptui_post_register_taxonomiesアクションの使用方法・解説

概要

cptui_post_register_taxonomiesは、Custom Post Type UIプラグインによって提供されるアクションフックであり、カスタムタクソノミーを登録するための機会を提供します。このフックは、特に以下のような機能を実装する際によく使われます。

  1. カスタムポストタイプに対するカスタムタクソノミーの追加
  2. 既存のタクソノミーの設定の変更
  3. タクソノミーに対するカスタム機能の追加
  4. 特定の条件に基づくタクソノミーの条件付け
  5. タクソノミーのラベルや表示条件の調整
  6. プラグインやテーマの設定によるタクソノミーの登録カスタマイズ

このフックは、Custom Post Type UIプラグインのバージョンに依存せず(2022年時点での最新バージョン)、WordPressのバージョンは4.0以上で動作します。

構文

add_action( 'cptui_post_register_taxonomies', 'your_function_name' );

function your_function_name() {
    // Code to execute
}

パラメータ

このアクションには、特定のパラメータはありませんが、カスタマイズとして必要な情報を配列や変数を通じて受け取ることが可能です。

戻り値

特に戻り値はありませんが、アクションを実行することで、タクソノミーを登録または修正することができます。

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

アクション名 使用例
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: カスタムタクソノミーを登録

このサンプルコードは、”Genre”という名前のカスタムタクソノミーを”Books”というカスタムポストタイプに追加します。

add_action( 'cptui_post_register_taxonomies', 'register_genre_taxonomy' );

function register_genre_taxonomy() {
    register_taxonomy( 'genre', 'books', array(
        'label' => 'Genres',
        'hierarchical' => true,
    ));
}

引用元: https://developer.wordpress.org/reference/functions/register_taxonomy/

サンプル2: タクソノミーのスラッグをカスタマイズ

このサンプルでは、”topic”というスラッグを持つカスタムタクソノミーを”Articles”というポストタイプに追加します。

add_action( 'cptui_post_register_taxonomies', 'register_topic_taxonomy' );

function register_topic_taxonomy() {
    register_taxonomy( 'topic', 'articles', array(
        'label' => 'Topics',
        'rewrite' => array( 'slug' => 'custom-topic' ),
    ));
}

引用元: https://developer.wordpress.org/reference/functions/register_taxonomy/

サンプル3: 複数のタクソノミーを一度に登録

このサンプルコードは、複数のカスタムタクソノミーを一度に登録します。

add_action( 'cptui_post_register_taxonomies', 'register_multiple_taxonomies' );

function register_multiple_taxonomies() {
    register_taxonomy( 'category', 'portfolio', array(
        'label' => 'Categories',
    ));

    register_taxonomy( 'tag', 'portfolio', array(
        'label' => 'Tags',
    ));
}

引用元: https://developer.wordpress.org/reference/functions/register_taxonomy/

サンプル4: カスタムタクソノミーのサポートオプションを追加

このサンプルコードでは、カスタムタクソノミーにサポートオプションを追加します。

add_action( 'cptui_post_register_taxonomies', 'add_taxonomy_support' );

function add_taxonomy_support() {
    register_taxonomy( 'team', 'members', array(
        'label' => 'Teams',
        'public' => true,
        'show_admin_column' => true,
        'hierarchical' => true,
    ));
}

引用元: https://developer.wordpress.org/reference/functions/register_taxonomy/

サンプル5: タクソノミーにカスタムフィールドを追加

このサンプルでは、タクソノミーにカスタムフィールドを設定する方法を示します。

add_action( 'cptui_post_register_taxonomies', 'add_custom_fields_to_taxonomy' );

function add_custom_fields_to_taxonomy() {
    register_taxonomy( 'location', 'events', array(
        'label' => 'Locations',
        'meta_box_cb' => 'custom_taxonomy_meta_box',
    ));
}

function custom_taxonomy_meta_box( $post, $box ) {
    // カスタムフィールドのフォームを表示
}

引用元: https://developer.wordpress.org/reference/functions/register_taxonomy/

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


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