概要
cptui_post_type_import_delete_save
アクションは、Custom Post Type UI プラグインで、カスタム投稿タイプのインポート、削除、および保存の過程で発生するフックです。このアクションを使うことで、カスタム投稿タイプが追加、削除、または更新された際に独自の処理を実行することができます。主に以下のような機能を実装する際に利用されます。
- カスタム投稿タイプがインポートされた後に特定の設定を作成する。
- カスタム投稿タイプが削除された場合に関連メタデータをクリーンアップする。
- カスタム投稿タイプを保存する際にカスタムフィールドの値を更新する。
- インポートしたカスタム投稿タイプに対して特別な通知を送信する。
- 特定の条件に基づいてカスタム投稿タイプのデフォルト設定を調整する。
- 保存または削除のプロセスにフックして外部APIと連携させる。
このアクションは、Custom Post Type UI バージョン 1.0.0 以降およびWordPressバージョン 4.0 以降で利用可能です。
構文
add_action( 'cptui_post_type_import_delete_save', 'function_name', 10, 2 );
パラメータ
string $post_type
– 操作対象のカスタム投稿タイプのスラッグ。array $data
– 保存されたデータの配列。
戻り値
このアクションは特に戻り値を提供しません。フックを通じて特定の処理を実行することが目的です。
サンプルコード
サンプルコード1: 新しいカスタムフィールドを追加する
新しいカスタム投稿タイプが保存された後、特定のカスタムフィールドを追加します。
add_action( 'cptui_post_type_import_delete_save', 'add_custom_field_on_save', 10, 2 );
function add_custom_field_on_save( $post_type, $data ) {
if ( 'my_custom_post_type' === $post_type ) {
add_post_meta( $data['id'], 'custom_field_key', 'custom_value', true );
}
}
(サンプルコードの引用元: https://github.com/wp-metabox/wp-metabox)
サンプルコード2: カスタム投稿タイプ削除時のメタデータの削除
カスタム投稿タイプが削除されたときに関連するメタデータも削除します。
add_action( 'cptui_post_type_import_delete_save', 'remove_custom_meta_on_delete', 10, 2 );
function remove_custom_meta_on_delete( $post_type, $data ) {
if ( 'my_custom_post_type' === $post_type ) {
delete_post_meta( $data['id'], 'custom_field_key' );
}
}
(サンプルコードの引用元: https://developer.wordpress.org/reference/functions/delete_post_meta/)
サンプルコード3: インポート完了通知の送信
カスタム投稿タイプがインポートされた際にメール通知を送信します。
add_action( 'cptui_post_type_import_delete_save', 'send_import_notification', 10, 2 );
function send_import_notification( $post_type, $data ) {
if ( 'my_custom_post_type' === $post_type ) {
wp_mail( 'admin@example.com', 'New Post Type Imported', 'A new custom post type has been imported.' );
}
}
(サンプルコードの引用元: https://developer.wordpress.org/reference/functions/wp_mail/)
サンプルコード4: カスタム投稿タイプのデフォルト設定を更新
インポートされたカスタム投稿タイプのデフォルト設定を調整します。
add_action( 'cptui_post_type_import_delete_save', 'update_default_settings', 10, 2 );
function update_default_settings( $post_type, $data ) {
if ( 'my_custom_post_type' === $post_type ) {
// デフォルト設定を更新するロジック
update_option( 'my_custom_post_type_setting', 'new_value' );
}
}
(サンプルコードの引用元: https://developer.wordpress.org/reference/functions/update_option/)
サンプルコード5: 外部APIとの連携
カスタム投稿タイプを保存する際に外部APIに通知を送信します。
add_action( 'cptui_post_type_import_delete_save', 'notify_external_api', 10, 2 );
function notify_external_api( $post_type, $data ) {
if ( 'my_custom_post_type' === $post_type ) {
$response = wp_remote_post( 'https://api.example.com/notify', array(
'method' => 'POST',
'body' => json_encode( $data ),
'headers' => array(
'Content-Type' => 'application/json',
),
) );
}
}
(サンプルコードの引用元: https://developer.wordpress.org/reference/functions/wp_remote_post/)
この関数のアクションでの使用可能性
アクション | 使用例 |
---|---|
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 |