概要
cptui_after_delete_post_type
フックは、WordPressのCustom Post Type UIプラグインに関連するアクションです。このフックは、カスタム投稿タイプが削除された後に実行されます。主に次のような機能でよく使用されます。
- 削除イベントに対するログ記録
- 関連するカスタムフィールドのクリーンアップ
- リダイレクト処理の実行
- 削除した投稿タイプに関連するデータベースのエントリを削除
- 別のカスタムアクションをトリガー
- 管理者への通知の送信
構文
add_action('cptui_after_delete_post_type', 'your_function_name', 10, 1);
パラメータ
$post_type
: 削除されたカスタム投稿タイプのスラッグ。
戻り値
このフックは戻り値を持っていません。
プラグインとWordPressのバージョン
- Custom Post Type UIバージョン: 1.10.0 以上
- WordPressバージョン: 5.0 以上
サンプルコード
サンプル 1: 削除イベントのログ記録
このコードは、削除されたカスタム投稿タイプのスラッグをログに記録します。
add_action('cptui_after_delete_post_type', 'log_post_type_deletion');
function log_post_type_deletion($post_type) {
error_log("カスタム投稿タイプ '{$post_type}' が削除されました。");
}
引用元: https://developer.wordpress.org/
サンプル 2: 関連するカスタムフィールドのクリーンアップ
削除されたカスタム投稿タイプに関連するカスタムフィールドを削除します。
add_action('cptui_after_delete_post_type', 'remove_custom_fields');
function remove_custom_fields($post_type) {
global $wpdb;
$wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE post_id IN (SELECT ID FROM {$wpdb->posts} WHERE post_type = '$post_type')");
}
引用元: https://developer.wordpress.org/
サンプル 3: リダイレクト処理
削除後に特定のページにリダイレクトします。
add_action('cptui_after_delete_post_type', 'redirect_after_deletion');
function redirect_after_deletion($post_type) {
wp_redirect(home_url('/custom-post-types-deleted/'));
exit;
}
引用元: https://developer.wordpress.org/
サンプル 4: データベースエントリの削除
関連するエントリをデータベースから削除します。
add_action('cptui_after_delete_post_type', 'delete_related_entries');
function delete_related_entries($post_type) {
global $wpdb;
// 例: 'related_table'というテーブルから関連するエントリを削除
$wpdb->delete('related_table', array('post_type' => $post_type));
}
引用元: https://developer.wordpress.org/
サンプル 5: 管理者への通知の送信
管理者に削除されたカスタム投稿タイプの通知を送信します。
add_action('cptui_after_delete_post_type', 'notify_admin_on_deletion');
function notify_admin_on_deletion($post_type) {
$admin_email = get_option('admin_email');
wp_mail($admin_email, "カスタム投稿タイプ削除通知", "カスタム投稿タイプ '{$post_type}' が削除されました。");
}
引用元: https://developer.wordpress.org/
この関数のアクションでの使用可能性
アクション | 使用可否 |
---|---|
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 |