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

概要

cptui_loadedは、WordPressのCustom Post Type UIプラグインに関連するアクションフックです。このアクションは、Custom Post Type UIプラグインが読み込まれた後に実行されるコードを追加することができます。cptui_loadedは、Custom Post Type UIの機能を拡張したり、他のプラグインとの統合を行う際に非常に便利です。具体的には、以下のような機能を実装する際によく使用されます。

  1. カスタム投稿タイプの登録
  2. カスタムメタボックスの追加
  3. カスタムタクソノミーの設定
  4. 投稿タイプの表示設定の変更
  5. アクションやフィルターの追加
  6. Custom Post Type UIの設定変更

構文

add_action('cptui_loaded', 'your_function_name');

パラメータ

  • なし

戻り値

  • なし

WordPressのバージョン

  • 5.0 以降

Custom Post Type UIのバージョン

  • 1.0 以降

サンプルコード

サンプル1: カスタム投稿タイプの作成

このコードは、Custom Post Type UIが読み込まれた後にカスタム投稿タイプ「portfolio」を追加します。

add_action('cptui_loaded', function() {
    register_post_type('portfolio', [
        'labels' => [
            'name' => 'Portfolios',
            'singular_name' => 'Portfolio'
        ],
        'public' => true,
        'has_archive' => true
    ]);
});

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

サンプル2: カスタムフィールドの追加

このコードは、ポートフォリオ投稿タイプにカスタムフィールドを追加します。

add_action('cptui_loaded', function() {
    if (function_exists('register_field_group')) {
        register_field_group([
            'id' => 'acf_portfolio-details',
            'title' => 'Portfolio Details',
            'fields' => [
                [
                    'key' => 'field_1',
                    'label' => 'Client Name',
                    'name' => 'client_name',
                    'type' => 'text'
                ]
            ],
            'location' => [
                [
                    [
                        'param' => 'post_type',
                        'operator' => '==',
                        'value' => 'portfolio'
                    ]
                ]
            ]
        ]);
    }
});

引用元: https://www.advancedcustomfields.com/resources/register-fields-via-code/

サンプル3: カスタムタクソノミーの追加

このコードは、ポートフォリオ投稿タイプにカスタムタクソノミー「ジャンル」を追加します。

add_action('cptui_loaded', function() {
    register_taxonomy('portfolio-category', 'portfolio', [
        'labels' => [
            'name' => 'Portfolio Categories',
            'singular_name' => 'Portfolio Category'
        ],
        'hierarchical' => true,
    ]);
});

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

サンプル4: 投稿タイプを管理画面に表示

このコードは、カスタム投稿タイプ「portfolio」を管理画面のメニューに追加します。

add_action('cptui_loaded', function() {
    add_menu_page('Portfolio', 'Portfolio', 'manage_options', 'edit.php?post_type=portfolio', '', 'dashicons-format-gallery');
});

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

サンプル5: 投稿タイプのサポートの追加

このコードは、カスタム投稿タイプにサムネイル機能を追加します。

add_action('cptui_loaded', function() {
    add_post_type_support('portfolio', 'thumbnail');
});

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

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

アクション 使用例
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

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


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