You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

咨询WordPress后台删除特定Contact Form 7表单的可用钩子

Handling Custom Cleanup When Deleting Contact Form 7 Forms

Great question! When you need to clean up related database entries right after a Contact Form 7 form is deleted from the WordPress admin, the ideal hook to use is wpcf7_after_delete_form.

This action hook fires after a CF7 form has been successfully deleted from the system, and it passes the deleted form object as a parameter—so you can easily grab the form's ID to target your custom data for removal.

Example Implementation

Here's a practical code snippet to add to your theme's functions.php file or a custom plugin:

// Hook into CF7 form deletion to clean up related data
add_action( 'wpcf7_after_delete_form', 'custom_cf7_cleanup_related_data', 10, 1 );

function custom_cf7_cleanup_related_data( $contact_form ) {
    // Retrieve the ID of the deleted form
    $deleted_form_id = $contact_form->id();

    // Replace this with your actual cleanup logic
    global $wpdb;
    // Delete entries from your custom database table linked to the deleted form
    $wpdb->delete(
        $wpdb->prefix . 'your_custom_table_name', // Your custom table (add WordPress prefix)
        array( 'cf7_form_id' => $deleted_form_id ), // Match the column linking to CF7 form IDs
        array( '%d' ) // Data type (integer for form ID)
    );
}

Key Notes:

  • If you need to run checks or perform actions before the form is deleted (like validating dependencies), you can use the wpcf7_before_delete_form hook instead. However, wpcf7_after_delete_form is safer for cleanup since it guarantees the form was actually removed from the system.
  • Always use WordPress's built-in database functions (like $wpdb->delete) instead of raw SQL queries to maintain security and compatibility across different WordPress setups.

内容的提问来源于stack exchange,提问作者Jaydip Shingala

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 03:52:57