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';
}
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.