contact form ajax error response

Question

I have this code that is supposed to stop the form if the nonce isn’t correct. I’m not able to test my contact form, I will get the ‘Errore’ response and the emails are not sended. Is there a fix to the code?

 function uptheme_submit_contact_form()
  {
    if( !isset($_POST['send_message']) || !wp_verify_nonce( $_POST['send_message'], 'wp_form_nonce' ) ){
      echo 'Errore';
      exit;
    }
    $fname = sanitize_text_field($_POST['fname']);
    $lname = sanitize_text_field($_POST['lname']);
    $email = sanitize_email($_POST['email']);
    $subject = sanitize_text_field($_POST['subject']);
    $message = sanitize_textarea_field($_POST['message']);

    $to = 'testmail@provider.org';
    $headers[] = "From: $site_url <$email>";
    wp_mail( $to, $subject, $message, $headers );
  }
  add_action( 'wp_ajax_send_message', 'uptheme_submit_contact_form' );
  add_action( 'wp_ajax_nopriv_send_message', 'uptheme_submit_contact_form' );

The action that is set inside the form is <input type="hidden" name="action" value="send_message" /> and the nonce field is wp_nonce_field('send_message', 'wp_form_nonce');

0
, , , sialfa 6 months 0 Answers 60 views 0

Leave an answer