Title: wpcf7_mail_sent infinite loop
Last modified: August 20, 2016

---

# wpcf7_mail_sent infinite loop

 *  [ltarranza](https://wordpress.org/support/users/frost_entangle/)
 * (@frost_entangle)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/wpcf7_mail_sent-infinite-loop/)
 * I created a couple of functions to trigger once the contact form is sent. This
   function generates a unique reference number and saves it on a database table
   together with a couple of posted data. The form successfully sends a mail and
   a database row is also added successfully however there is a weird occurrence
   on the form page, it does not go past beyond the loading icon and does not show
   up the ‘sent successfully’ message. Aside from that, the page does not get redirected
   to the thank you page which I set using on_sent_ok.
    ` add_action( 'wpcf7_mail_sent','
   your_wpcf7_mail_sent_function' );
 * function gen_ref( $ref_name = 'X X') {
    $int_ren_name = split(" ",preg_replace('/\
   s\s+/', ' ', trim($ref_name))); $fname = strtoupper($int_ren_name[0]); $sname
   = (count($int_ren_name)>1) ? strtoupper($int_ren_name[1]) : $fname{1};
 *  return date("U").$fname{0}.$sname{0};
    }
 * function your_wpcf7_mail_sent_function( $contact_form ) {
    global $wpdb; $title
   = $contact_form->title; $posted_data = $contact_form->posted_data;
 *  if ( 'Contact Us Form' == $title ) {
    $name = $posted_data['name']; $surname
   = $posted_data['surname']; $email = $posted_data['email']; $ref_name = $name."".
   $surname;
 *  $referenceNumber = gen_ref($ref_name);
 *  $sql = $wpdb->prepare("INSERT INTO `wp_formdata`(`reference_id`, `name`, `surname`,`
   email`) VALUES (%d,%s,%s,%s)”, $referenceNumber, $name, $surname, $email);
 *  $wpdb->query($sql);
 *  }
    }
 * [http://wordpress.org/extend/plugins/contact-form-7/](http://wordpress.org/extend/plugins/contact-form-7/)

Viewing 8 replies - 1 through 8 (of 8 total)

 *  [biastechnology](https://wordpress.org/support/users/biastechnology/)
 * (@biastechnology)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/wpcf7_mail_sent-infinite-loop/#post-3152252)
 * We are looking to do something very similar. Where did you place this code?
 * Within the functions.php or within on_sent_ok: “”
 * Technically we need to do it slightly differently:-
 * Submit
    Insert Data into DB Return Index_ID Compile email with Index_ID Send 
   Email Success Screen.
 * I’ll have a play this end and let you know how I got on
 *  Thread Starter [ltarranza](https://wordpress.org/support/users/frost_entangle/)
 * (@frost_entangle)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/wpcf7_mail_sent-infinite-loop/#post-3152253)
 * I have it on functions.php. The process works fine, it sends the email and saves
   a row on the DB. However it could not get past the loading icon beside the button
   and display the success/failure message.
 *  [biastechnology](https://wordpress.org/support/users/biastechnology/)
 * (@biastechnology)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/wpcf7_mail_sent-infinite-loop/#post-3152258)
 * This may be of use:-
 * [How to: Redirect after submit](http://wordpress.org/support/topic/plugin-contact-form-7-how-to-redirect-after-submit?replies=1)
 *  Thread Starter [ltarranza](https://wordpress.org/support/users/frost_entangle/)
 * (@frost_entangle)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/wpcf7_mail_sent-infinite-loop/#post-3152259)
 * That looks good, but that doesn’t solve the issue. I know there is an issue because
   when I tested it before I made the changes above, the form first displays the
   usual green success notification that the email was sent and then it redirects
   the page to the page I set using _on\_sent\_ok_. The post you shared above would
   probably skip the green success message and just redirect to the page.
 *  Thread Starter [ltarranza](https://wordpress.org/support/users/frost_entangle/)
 * (@frost_entangle)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/wpcf7_mail_sent-infinite-loop/#post-3152289)
 * Anyone else?
 *  [HarrisITServices](https://wordpress.org/support/users/harrisitservices/)
 * (@harrisitservices)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/wpcf7_mail_sent-infinite-loop/#post-3152299)
 * I am having the same issue and cannot figure it out. I have tried the following
   two snippets of code in functions.php and the email sends but the page never 
   redirects. It just stays with the little spinning circle next to the submit button.
   If anyone has figured this out please let me know as this is driving me nuts.
 *     ```
       add_action('wpcf7_mail_sent', 'wpcf7_redirect_on_submit');
       function wpcf7_redirect_on_submit($wpcf7)
       {
       		wp_redirect( 'http://www.google.com' );
       		exit;
       }
       ```
   
 *     ```
       add_action('wpcf7_mail_sent', 'wpcf7_redirect_on_submit');
       function wpcf7_redirect_on_submit($wpcf7)
       {
       		header( 'Location: http://www.google.com' );
       		exit;
       }
       ```
   
 *  [HarrisITServices](https://wordpress.org/support/users/harrisitservices/)
 * (@harrisitservices)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/wpcf7_mail_sent-infinite-loop/#post-3152303)
 * Anyone have any ideas? The only solution I could find was to turn off ajax. However,
   for me this is not a solution. Does anyone know how to use the wpcf7 hook to 
   redirect without turning ajax off?
 *  [kg69design](https://wordpress.org/support/users/kg69design/)
 * (@kg69design)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/wpcf7_mail_sent-infinite-loop/#post-3152307)
 * I suppose that we can’t use “output” here like “echo”, “header” etc.

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘wpcf7_mail_sent infinite loop’ is closed to new replies.

 * ![](https://ps.w.org/contact-form-7/assets/icon.svg?rev=2339255)
 * [Contact Form 7](https://wordpress.org/plugins/contact-form-7/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/contact-form-7/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/contact-form-7/)
 * [Active Topics](https://wordpress.org/support/plugin/contact-form-7/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/contact-form-7/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/contact-form-7/reviews/)

 * 8 replies
 * 4 participants
 * Last reply from: [kg69design](https://wordpress.org/support/users/kg69design/)
 * Last activity: [13 years, 2 months ago](https://wordpress.org/support/topic/wpcf7_mail_sent-infinite-loop/#post-3152307)
 * Status: not resolved