$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>ajax - wp_mail doesn't work when logged in?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

ajax - wp_mail doesn't work when logged in?

matteradmin9PV0评论

I am having trouble with a wp_mail function I created, which sends an email when I press a button. It does work great when you do this logged out. But as soon as I am logged in it stops working. It is not a capability issue, as I get the error as a Super Admin also.

Why would this happen? I am using a PHP function together with Ajax to send the email, Javascript function is called onclick.

function searchEmail(email,title,content,location) {
  var admin_url = admin_ajax.ajaxurl;
  $.ajax({
    type: "POST",
    url: admin_url,
    datatype: "html",
    data: { 'action': 'search_notify_email', email: email, title: title, content: content, location: location },
    success: function() {
      searchNotification();
    },error:function() {
      searchNotificationError();
    }
  });
}

PHP:

function search_notify_email() {
  // Set variables
  $email = $_POST['email'];
  $title = $_POST['title'];
  $content = $_POST['content'];
  $location = $_POST['location'];
  // Change Email to HTML
  add_filter( 'wp_mail_content_type', 'set_email_content_type' );
    $to = $email;
    $subject = "Test subject!";
    $message = "<img src='favicon.png'><br><b>Test!</b>";
    if (empty($title)) {
      $message .= "<br><br><b>" . $_POST['content'] . "</b> test.<br> ";
    }
    else {
      $message .= "<br><br><b>" . $_POST['content'] . "</b> test " . $_POST['title'] . " test.<br> ";
    }
    if (!empty($location)) {
      $message .= "Test <b>" . $_POST['location'] . "</b>";
    }
    $headers[] = 'From: Testing <[email protected]>';

    if ( wp_mail($to, $subject, $message, $headers) ) {
      // Success
    } else {
      // Error
    }
    die();
    // Remove filter HTML content type
    remove_filter( 'wp_mail_content_type', 'set_email_content_type' );
}
add_action('wp_ajax_nopriv_search_notify_email', 'search_notify_email');
add_action('wp_ajax_search_notify_emaill', 'search_notify_email');

// Reset Email content type to standard text/html
function set_email_content_type() {
    return 'text/html';
}

I am having trouble with a wp_mail function I created, which sends an email when I press a button. It does work great when you do this logged out. But as soon as I am logged in it stops working. It is not a capability issue, as I get the error as a Super Admin also.

Why would this happen? I am using a PHP function together with Ajax to send the email, Javascript function is called onclick.

function searchEmail(email,title,content,location) {
  var admin_url = admin_ajax.ajaxurl;
  $.ajax({
    type: "POST",
    url: admin_url,
    datatype: "html",
    data: { 'action': 'search_notify_email', email: email, title: title, content: content, location: location },
    success: function() {
      searchNotification();
    },error:function() {
      searchNotificationError();
    }
  });
}

PHP:

function search_notify_email() {
  // Set variables
  $email = $_POST['email'];
  $title = $_POST['title'];
  $content = $_POST['content'];
  $location = $_POST['location'];
  // Change Email to HTML
  add_filter( 'wp_mail_content_type', 'set_email_content_type' );
    $to = $email;
    $subject = "Test subject!";
    $message = "<img src='favicon.png'><br><b>Test!</b>";
    if (empty($title)) {
      $message .= "<br><br><b>" . $_POST['content'] . "</b> test.<br> ";
    }
    else {
      $message .= "<br><br><b>" . $_POST['content'] . "</b> test " . $_POST['title'] . " test.<br> ";
    }
    if (!empty($location)) {
      $message .= "Test <b>" . $_POST['location'] . "</b>";
    }
    $headers[] = 'From: Testing <[email protected]>';

    if ( wp_mail($to, $subject, $message, $headers) ) {
      // Success
    } else {
      // Error
    }
    die();
    // Remove filter HTML content type
    remove_filter( 'wp_mail_content_type', 'set_email_content_type' );
}
add_action('wp_ajax_nopriv_search_notify_email', 'search_notify_email');
add_action('wp_ajax_search_notify_emaill', 'search_notify_email');

// Reset Email content type to standard text/html
function set_email_content_type() {
    return 'text/html';
}
Share Improve this question edited Feb 14, 2019 at 13:59 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Feb 14, 2019 at 13:44 joq3joq3 3813 silver badges21 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You've got a typo in the hook that runs for logged in users:

add_action('wp_ajax_search_notify_emaill', 'search_notify_email');

There's an extra l in the hook name. The hook names need to be the same, apart from nopriv_, so you should have:

add_action('wp_ajax_nopriv_search_notify_email', 'search_notify_email');
add_action('wp_ajax_search_notify_email', 'search_notify_email');

When you send an AJAX request with an action WordPress runs the hook wp_ajax_{$action} (where $action is the parameter passed with the request) if you're logged in, or wp_ajax_nopriv_{$action} if the user is not logged in. Since you'd used an incorrect hook name for the logged-in version (due to the typo) your search_notify_email() function wasn't hooked to run when the user was logged in.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far