Support » Developing with WordPress » wp_mail sending twice

  • So I have created a custom plugin for a site I am working on. The output is controlled by a shortcode that is in the content area of a page. When the form with a nonce gets filled out and is submitted vie POST method, as the shortcode fires it then checks the submission of the button, nonce, and data and then sends out an wp_mail email. Issue I am having is that it emails the exact same email twice almost like the shortcode is firing twice but only displays the output. I am not sure what is happening.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Here is the gist of the code that is being used.

    function form_func() {
      $error_msg = '';
      if (isset($_POST['form_submit'])) {
        if (!wp_verify_nonce($retrieved_nonce, 'send-form') ) {
          $error_msg = "not sent";
        } else {
          $email_message = sanitize_text_field($_POST['email_message']);
          if ( wp_mail( '', 'subject', $email_message ) == true) {
            $error_msg = "email sent";
          } else {
            $error_msg = "email not sent";
      $returnHtml = "<div id='message'>" . $error_msg . "</div><form method='post'>" . wp_nonce_field('send-form') . '<input type="text" name="email_message"><input type="submit" name="form_submit"></form>';

    So to solve the problem I separated out the emailing part into an INIT action. But as to why I am unable to do this kind of checking in the shortcode function I do not know. So if anyone can possibly explain this that would be appreciated.

    Moderator bcworkz


    I’m unable to replicate the observed behavior. Emailing out of my shortcode only occurs once. There’s something unique about your installation. It’s conceivable your theme or one of your plugins is getting post content for some reason, which triggers the shortcode. Output can be buffered, so lack of output tells us nothing. Since you found a workaround, it’s probably not worth further investigation.

    If you wanted to investigate, you could have the code throw a user error, then study the backtrace, assuming the phantom call happens before actual output.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘wp_mail sending twice’ is closed to new replies.