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;
    type: "POST",
    url: admin_url,
    datatype: "html",
    data: { 'action': 'search_notify_email', email: email, title: title, content: content, location: location },
    success: function() {
    },error:function() {


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 <noreply@example.com>';

    if ( wp_mail($to, $subject, $message, $headers) ) {
      // Success
    } else {
      // Error
    // 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';

1 Answer 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.

  • Oh, thank you for catching that! I am very glad it was something this simple. However without your help I would probably have spent many more hours debugging this!
    – joq3
    Feb 14, 2019 at 14:42
  • I have another question, slightly related to this. It is the same function. The content is sent to the javascript onclik with this echo get_the_content(); which works great. Except when there are multiple lines in the content, then nothing will happen. Why is that? the_content doesn't work.
    – joq3
    Feb 14, 2019 at 19:49
  • I created a new thread for this error here: wordpress.stackexchange.com/questions/328749/…
    – joq3
    Feb 14, 2019 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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