Adding the post author as a recipient

This guide will show you how to add the email address of the author of the post containing the form as a recipient of the notification email. Add the following code to the WP theme functions.php file (or create a plugin for it).

12
3
4
5
6
7
8
9
10
11
12
13
add_action('quform_pre_send_notification_1_1', function (PHPMailer $mailer) {    $postId = isset($_POST['post_id']) ? (int) $_POST['post_id'] : 0;
    $post = get_post($postId);
 
    if ($post instanceof WP_Post) {
        $author = get_user_by('id', $post->post_author);
 
        if ($author instanceof WP_User && Quform_Validator_Static::isValid('email', $author->user_email)) {
            $mailer->clearAddresses();
            $mailer->addAddress($author->user_email);
        }
    }
});
add_action('quform_pre_send_notification_1_1', function (PHPMailer $mailer) {
    $postId = isset($_POST['post_id']) ? (int) $_POST['post_id'] : 0;
    $post = get_post($postId);

    if ($post instanceof WP_Post) {
        $author = get_user_by('id', $post->post_author);

        if ($author instanceof WP_User && Quform_Validator_Static::isValid('email', $author->user_email)) {
            $mailer->clearAddresses();
            $mailer->addAddress($author->user_email);
        }
    }
});
  • On line 1, replace 1_1 with the notification unique ID

Note: this code will remove the To recipient(s) that are set in the form builder, if you want to keep these recipients remove line 9.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function my_pre_send_notification(PHPMailer $mailer)
{
    $postId = isset($_POST['post_id']) ? (int) $_POST['post_id'] : 0;
    $post = get_post($postId);
 
    if ($post instanceof WP_Post) {
        $author = get_user_by('id', $post->post_author);
 
        if ($author instanceof WP_User && Quform_Validator_Static::isValid('email', $author->user_email)) {
            $mailer->clearAddresses();
            $mailer->addAddress($author->user_email);
        }
    }
}
add_action('quform_pre_send_notification_1_1', 'my_pre_send_notification');
function my_pre_send_notification(PHPMailer $mailer)
{
    $postId = isset($_POST['post_id']) ? (int) $_POST['post_id'] : 0;
    $post = get_post($postId);

    if ($post instanceof WP_Post) {
        $author = get_user_by('id', $post->post_author);

        if ($author instanceof WP_User && Quform_Validator_Static::isValid('email', $author->user_email)) {
            $mailer->clearAddresses();
            $mailer->addAddress($author->user_email);
        }
    }
}
add_action('quform_pre_send_notification_1_1', 'my_pre_send_notification');
  • On line 15, replace 1_1 with the notification unique ID

Note: this code will remove the To recipient(s) that are set in the form builder, if you want to keep these recipients remove line 10.

Be inspired. © 2018 ThemeCatcher Ltd. 20-22 Wenlock Road, London, England, N1 7GU | Company No. 08120384 | Built with React | Privacy Policy