求助:为Contact Form 7创建hook,实现付款后开启skip_mail发送表单邮件
skip_mail) Got it! You’re totally on the right track using skip_mail = on to block immediate form emails, and we can absolutely use a custom hook to flip that setting and send the form data only after a successful payment goes through. Here’s a step-by-step solution tailored to your setup:
1. Use the Add-on’s Payment Success Hook
The PayPal & Stripe Add-on for Contact Form 7 fires specific action hooks when a payment completes successfully. The most reliable one (works for both gateways) is wpcf7_payment_completed—we’ll use this to trigger our email logic.
2. Add the Custom Code
You’ll need to drop this snippet into your WordPress theme’s functions.php file, or better yet, a custom plugin (to avoid losing code when you update your theme):
// Trigger CF7 email after successful payment add_action( 'wpcf7_payment_completed', 'send_cf7_email_post_payment', 10, 2 ); function send_cf7_email_post_payment( $payment_details, $cf7_submission ) { // Grab the CF7 form object from the submission data $cf7_form = $cf7_submission->get_contact_form(); // Temporarily turn off skip_mail so the email can send $cf7_form->set_properties( array( 'skip_mail' => false ) ); // Fetch the form's email settings and send the mail $email_settings = $cf7_form->prop( 'mail' ); $cf7_form->send_mail( $cf7_submission, $email_settings ); // Optional: Flip skip_mail back to on if you want future submissions to still skip by default $cf7_form->set_properties( array( 'skip_mail' => true ) ); }
3. Key Notes & Troubleshooting
- Double-check the hook name: If you’re using an older version of the add-on, confirm the hook in its documentation—some versions use gateway-specific hooks like
wpcf7_stripe_payment_completedorwpcf7_paypal_payment_completedif you need to target one payment method only. - Test with sandbox payments: Always run a test payment (using PayPal Sandbox or Stripe Test Mode) to make sure the email sends exactly when you expect it to.
- Adjust priority if needed: If you have other CF7-related hooks running, tweak the
10inadd_actionto a higher number (like20) if this code isn’t firing correctly—this ensures it runs after other conflicting logic. - Customize the email: If you want the post-payment email to look different from your default CF7 email, you can use the
wpcf7_mail_componentsfilter inside this function to modify the subject, body, or recipients.
内容的提问来源于stack exchange,提问作者nubitol

