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

概要

cptui_third_party_post_type_importアクションは、WordPressのプラグイン「Custom Post Type UI」に関連しており、カスタム投稿タイプのインポート機能を拡張するためのフックです。このアクションは、外部のカスタム投稿タイプをインポートする際に、追加の処理やカスタマイズを行うためのポイントを提供します。具体的には、次のような機能の実装に役立ちます。

  1. カスタム投稿タイプに必要な初期データの設定
  2. 投稿タイプのメタデータを一括インポートする機能
  3. 外部ソースからカスタム投稿タイプのデータを取り込む際のフィルタリング
  4. インポート後のカスタム投稿タイプのデフォルト設定の調整
  5. 他のプラグインとの連携を強化するための拡張ポイント
  6. インポートしたデータのバリデーションやクレンジング

構文

do_action( 'cptui_third_party_post_type_import', $post_type_data );

パラメータ

  • $post_type_data: インポートするカスタム投稿タイプのデータ配列。

戻り値

戻り値はありませんが、フック内で処理を加えることができます。

使用可能なバージョン

  • Custom Post Type UI: 1.0以上
  • WordPress: 4.0以上

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

アクション 使用例
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_action('cptui_third_party_post_type_import', 'add_custom_meta_on_import');

function add_custom_meta_on_import($post_type_data) {
    if (!empty($post_type_data['name'])) {
        $post_id = wp_insert_post(array(
            'post_title' => $post_type_data['name'],
            'post_type' => $post_type_data['post_type'],
            'post_status' => 'publish'
        ));
        add_post_meta($post_id, 'custom_meta_key', 'custom_meta_value');
    }
}

引用元: https://developer.wordpress.org/

サンプルコード 2: インポート後にカスタムタクソノミーを登録

このコードは、インポート完了後にカスタムタクソノミーを登録します。

add_action('cptui_third_party_post_type_import', 'register_custom_taxonomy_after_import');

function register_custom_taxonomy_after_import($post_type_data) {
    if ($post_type_data['post_type'] === 'my_custom_post') {
        register_taxonomy('my_custom_taxonomy', 'my_custom_post', array(
            'label' => __('My Custom Taxonomy'),
            'rewrite' => array('slug' => 'my-custom-taxonomy'),
            'hierarchical' => true,
        ));
    }
}

引用元: https://developer.wordpress.org/

サンプルコード 3: インポートをカスタマイズする

このコードは、特定の条件に基づいてインポートするカスタム投稿の内容を調整します。

add_action('cptui_third_party_post_type_import', 'customize_import_process');

function customize_import_process($post_type_data) {
    if ($post_type_data['type'] === 'special') {
        $post_type_data['title'] = 'Custom Title';
    }
    return $post_type_data;
}

引用元: https://developer.wordpress.org/

サンプルコード 4: インポート後にリダイレクトする

このコードは、インポートが完了した後に特定のページにリダイレクトします。

add_action('cptui_third_party_post_type_import', 'redirect_after_import');

function redirect_after_import($post_type_data) {
    if ($post_type_data['post_type'] === 'my_post_type') {
        wp_redirect(home_url('thank-you'));
        exit;
    }
}

引用元: https://developer.wordpress.org/

サンプルコード 5: インポートした投稿タイプに追加スタイルを適用

このコードは、インポートした投稿タイプに特別なスタイルを適用します。

add_action('cptui_third_party_post_type_import', 'apply_custom_style_to_imported_post');

function apply_custom_style_to_imported_post($post_type_data) {
    if ($post_type_data['post_type'] === 'styled_post') {
        add_filter('the_content', function($content) {
            return '<div class="custom-style">' . $content . '</div>';
        });
    }
}

引用元: https://developer.wordpress.org/

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


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