I've added a button in the backend of my custom post type editor.

I want to send an email using wp_mail() when admin user click a button.

I've added this custom form and button to a meta box. See screenshot.

The problem is that when clicking this button Wordpress redirects to the default backend posts list page (https://mysite.test/wp-admin/edit.php).

I am making use of the do_action( "admin_post_{$action}" ) hook.

When I inspect the page I see Wordpress has removed the tags.

Any help will be greatly appreciated.

Here is my code:

    $quote_add_meta_nonce = wp_create_nonce('send_quote_email_form_nonce');

    add_action('admin_post_quote_email_pdf', 'quote_email_pdf');

    function quote_email_pdf()
        if (isset($_POST['send_quote_email_meta_nonce']) && wp_verify_nonce($_POST['send_quote_email_meta_nonce'], 'send_quote_email_form_nonce')) {
            $emailMessage = '<h1>It works</h1>';
            echo $emailMessage;
        } else {
            echo 'Something went wrong';

<form action="<?php echo esc_url(admin_url('admin-post.php')); ?>" method="POST">
    <input type="hidden" name="action" value="quote_email_pdf">
    <input type="hidden" name="send_quote_email_meta_nonce" value="<?php echo $quote_add_meta_nonce ?>" />
    <input type="submit" value="Email quote to customer" class="button">
  • Did I understand you correctly, you want to stay on the same page after the submit? So why not send the form via an Ajax call?
    – user141080
    Apr 2, 2022 at 20:54
  • I have posted this on Stackoverflow as well. Here is the link stackoverflow.com/questions/71583212/… I have indeed solved this using AJAX. Apr 4, 2022 at 6:55
  • I am glad to hear that you have found a solution. Please do not forget to accept the answer. Accept your own answers
    – user141080
    Apr 5, 2022 at 6:47

1 Answer 1


Solved this using AJAX. Here is my updated code:

add_action('wp_ajax_quote_email_pdf', 'quote_email_pdf');

function quote_email_pdf()
    wp_mail( $to, $subject, $message, $headers, $attachments );

<button class="button" id="downloadQuote">Send email</button>

  const fullName = $('#quoteFullName').text();
  const emailAddress = $('#quoteEmail a').text();

      url: sf_admin_ajax.sf_admin_ajax_url,
      type: 'POST',
      data: {
          action: 'quote_email_pdf',
          emailAddress: emailAddress,
          fullName: fullName
      beforeSend: function() {
      success: function(data, textStatus, XMLHttpRequest) {
      error: function ( MLHttpRequest, textStatus, errorThrown ) {


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.