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

概要

cptui_post_type_submit_editは、WordPressプラグイン「Custom Post Type UI」で使用されるフックです。このアクションは、カスタム投稿タイプを編集中に実行され、投稿タイプが正常に保存された後にカスタムコードを実行するために使用されます。これにより、開発者はユーザーが投稿タイプを保存する際に追加の処理を実行できます。

このアクションは以下のような機能を実装する際によく使われます:
1. カスタムフィールドのデータを保存する
2. 投稿タイプに関連するメタデータを更新する
3. タグやカテゴリのカスタム処理を実行する
4. 投稿が保存されたときに通知を送信する
5. 外部APIと通信してデータを同期する
6. 投稿の状態を変更する(例:下書きから公開へ)

構文

add_action('cptui_post_type_submit_edit', 'custom_function', 10, 2);

パラメータ

  • $post_type: 保存されたカスタム投稿タイプの名前
  • $post_id: 保存された投稿のID

戻り値

このアクションは値を返しませんが、指定した処理を実行します。

使用可能なバージョン

  • 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_post_type_submit_edit', 'save_custom_metadata', 10, 2);

function save_custom_metadata($post_type, $post_id) {
    if ($post_type === 'your_custom_post_type') {
        update_post_meta($post_id, 'custom_meta_key', 'custom_value');
    }
}

このサンプルコードは、特定のカスタム投稿タイプが保存されるたびに、カスタムメタデータを保存します。

サンプルコード2

add_action('cptui_post_type_submit_edit', 'notify_on_save', 10, 2);

function notify_on_save($post_type, $post_id) {
    if ($post_type === 'your_custom_post_type') {
        wp_mail('admin@example.com', 'カスタム投稿が保存されました', '投稿ID: ' . $post_id);
    }
}

このサンプルコードは、指定したカスタム投稿タイプが保存されると管理者にメール通知を送信します。

サンプルコード3

add_action('cptui_post_type_submit_edit', 'sync_with_external_api', 10, 2);

function sync_with_external_api($post_type, $post_id) {
    if ($post_type === 'your_custom_post_type') {
        // 外部APIとの通信処理を記述
    }
}

このサンプルコードは、カスタム投稿タイプが保存される際に外部APIとデータを同期するための処理の雛形です。

サンプルコード4

add_action('cptui_post_type_submit_edit', 'change_post_status', 10, 2);

function change_post_status($post_type, $post_id) {
    if ($post_type === 'your_custom_post_type') {
        $post = array(
            'ID' => $post_id,
            'post_status' => 'publish',
        );
        wp_update_post($post);
    }
}

このサンプルコードは、特定のカスタム投稿タイプが保存されると投稿の状態を「公開」に変更します。

サンプルコード5

add_action('cptui_post_type_submit_edit', 'update_category_terms', 10, 2);

function update_category_terms($post_type, $post_id) {
    if ($post_type === 'your_custom_post_type') {
        wp_set_object_terms($post_id, array('term1', 'term2'), 'category');
    }
}

このサンプルコードは、カスタム投稿タイプが保存される際にカテゴリの用語を更新します。

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


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