Dear Folks,
I am writing my first WordPress plugin and I am having a problem with the
wp_mail()
function. My plugin is intended to be used by a visitor to the site to send an invitation to a friend with a link to the website.
Here is the code:
<?php
if (!empty($_POST)) {
error_log("process form", 0);
if (isset($_POST['action']) && $_POST['action'] === 'send_email') {
send_email();
return "Your email has been sent successfully";
}
else {
error_log("Email could not be sent", 0);
}
}
function send_email() {
$subject = 'test';
$message = 'this is a test from send invitation';
$to = my_email@example.com ;
wp_mail($to, $subject, $message);
}
?>
The code runs successfully until it meets
wp_mail()
and prints in the error log that it is undefined. I know that pluggables are loaded after all plugins are loaded and one must use add_action to have it defined but I have seen many plugins where the
wp_mail()
function is used without using any
add_action()
declaration. If I were to
add_action('plugins_loaded', send_email)
the function is defined but it fires of an email on every page load which is not what I want.
There is a gap in my understanding of how things are working here and I hope someone could shed some light help me out with my code.
Thanks in advance.
nav