Adding a recipient from a custom field

This guide will show you how to add an email address from a custom field 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
34
5
6
7
8
9
add_action('quform_pre_send_notification_1_1', function ($mailer) {    $postId = isset($_POST['post_id']) ? (int) $_POST['post_id'] : 0;
    $email = get_post_meta($postId, 'my_custom_email_field', true); 
    if (Quform_Validator_Static::isValid('email', $email)) {
        $mailer->clearAddresses();
        $mailer->addAddress($email);
    }
});
add_action('quform_pre_send_notification_1_1', function ($mailer) {
    $postId = isset($_POST['post_id']) ? (int) $_POST['post_id'] : 0;
    $email = get_post_meta($postId, 'my_custom_email_field', true);

    if (Quform_Validator_Static::isValid('email', $email)) {
        $mailer->clearAddresses();
        $mailer->addAddress($email);
    }
});
  • On line 1, replace 1_1 with the notification unique ID
  • On line 3, replace my_custom_email_field with the name of the custom field

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 6.

1
2
3
45
6
7
8
9
10
11
function my_pre_send_notification($mailer)
{
    $postId = isset($_POST['post_id']) ? (int) $_POST['post_id'] : 0;
    $email = get_post_meta($postId, 'my_custom_email_field', true); 
    if (Quform_Validator_Static::isValid('email', $email)) {
        $mailer->clearAddresses();
        $mailer->addAddress($email);
    }
}
add_action('quform_pre_send_notification_1_1', 'my_pre_send_notification');
function my_pre_send_notification($mailer)
{
    $postId = isset($_POST['post_id']) ? (int) $_POST['post_id'] : 0;
    $email = get_post_meta($postId, 'my_custom_email_field', true);

    if (Quform_Validator_Static::isValid('email', $email)) {
        $mailer->clearAddresses();
        $mailer->addAddress($email);
    }
}
add_action('quform_pre_send_notification_1_1', 'my_pre_send_notification');
  • On line 4, replace my_custom_email_field with the name of the custom field
  • On line 11, 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 7.

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