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

咨询:前端表单创建Custom Post Type及状态变更邮件通知实现方案

Solution for Sending Emails When Custom Post Type Status Changes

Hey there, let's break down how to fix that final email notification step you're stuck on. You've already got the frontend form creating a Custom Post Type (CPT) set to pending review—now we just need to trigger emails when you approve or cancel those submissions. Here are your best options, whether you prefer no-code plugins or custom code:

Option 1: No-Code Plugin Approach (Great for Non-Developers)

If you don't want to dive into code, these plugin combinations will get the job done smoothly:

  • Gravity Forms + Custom Post Types Add-On + Advanced Notifications

    • First, use the Custom Post Types Add-On to link your frontend form to your CPT, setting all new submissions to Pending Review by default.
    • Then, use the Advanced Notifications extension (or even core Gravity Forms notifications with conditional logic) to create rules: When the CPT's status changes to "Published" or "Trashed/Canceled", send an email to the submitter's address pulled directly from your form's email field.
  • WPForms + Custom Post Type Integration

    • Similar to Gravity Forms: Map your form fields to your CPT and set submissions to pending on submission.
    • Use WPForms' Notification Rules to trigger emails only when the post status shifts from pending to published or your defined "canceled" state (like draft or trashed, depending on your workflow).
  • Post Status Notifier Lite

    • This plugin is built specifically for tracking post status changes. Configure it to target your exact CPT, set triggers for "Post transitions from Pending Review to Published" or "Post transitions from Pending Review to Trashed", and set the recipient to the submitter's email (just make sure your form stores the submitter's email as a custom field on the CPT first).

Option 2: Custom Code Approach (Full Control for Developers)

If you want total flexibility, use WordPress core hooks to build the notification system yourself. Here's a step-by-step implementation:

Step 1: Store the Submitter's Email

First, ensure your frontend form saves the submitter's email as a custom meta field on the CPT. For example, if your form has an email field named submitter_email, add this line when creating the CPT post:

update_post_meta($post_id, '_submitter_email', sanitize_email($_POST['submitter_email']));

Step 2: Hook Into Post Status Changes

Use the transition_post_status hook to listen for status changes on your CPT, then send the email:

add_action('transition_post_status', 'send_cpt_submitter_notification', 10, 3);
function send_cpt_submitter_notification($new_status, $old_status, $post) {
    // Replace with your CPT's slug (e.g., "user_submission")
    $target_cpt = 'your_custom_post_type_slug';
    
    // Only run for your target CPT, and only when moving from Pending Review
    if ($post->post_type !== $target_cpt || $old_status !== 'pending') {
        return;
    }
    
    // Get the submitter's email from the custom meta field
    $submitter_email = get_post_meta($post->ID, '_submitter_email', true);
    if (!is_email($submitter_email)) {
        return; // Exit if no valid email exists
    }
    
    // Customize email content based on new status
    if ($new_status === 'publish') {
        $subject = 'Your Submission Has Been Published!';
        $message = sprintf(
            "Hi,\n\nYour submission titled '%s' has been approved and published.\n\nYou can view it here: %s\n\nThanks,\nYour Team",
            get_the_title($post->ID),
            get_permalink($post->ID)
        );
    } elseif ($new_status === 'trash') { // Adjust this to match your "canceled" status (e.g., "draft")
        $subject = 'Your Submission Has Been Canceled';
        $message = sprintf(
            "Hi,\n\nYour submission titled '%s' has been canceled.\n\nIf you have questions, feel free to reach out.\n\nThanks,\nYour Team",
            get_the_title($post->ID)
        );
    } else {
        return; // Exit if status isn't publish or canceled
    }
    
    // Send the email using WordPress core function
    wp_mail($submitter_email, $subject, $message);
}

Pro Tips for Custom Code

  • Replace your_custom_post_type_slug with your actual CPT slug.
  • Adjust the $new_status check to match how you define "canceled" (e.g., use draft instead of trash if that's your preferred workflow).
  • Use a plugin like WP Mail SMTP to ensure your emails don't end up in spam folders.

Learning Resources to Deepen Your Knowledge

  • WordPress Core Code Reference: Look up transition_post_status, wp_mail, and update_post_meta for detailed parameter explanations and examples.
  • WordPress Developer Handbook: Guides on Custom Post Types, post status transitions, and form handling in WordPress.
  • WPBeginner’s tutorials on CPT integration and email notifications (great for beginners looking for step-by-step walkthroughs).

内容的提问来源于stack exchange,提问作者Ahmed Elhanony

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:27:06