• Resolved schmicho

    (@schmicho)


    Hi there,

    I got a bit of a tricky situation. (At least from my perspective ?? )
    I’d like to use a custom field as the email recipient. So that I can use the same form but for different pages and people.

    Is that somehow possible ?
    Thanks ??

Viewing 13 replies - 1 through 13 (of 13 total)
  • Try to describe it better, I’ll be glad to help you!

    Thread Starter schmicho

    (@schmicho)

    I want to create several posts, each with unique content, but they’ll all share the same contact form. In the admin area, I’d like to assign a specific email address to each post using a custom field called ’email.’ Then, when someone fills out the contact form on any of these posts, the email should be sent to the address stored in the ’email’ custom field for that specific post.

    Thread Starter schmicho

    (@schmicho)

    I got this thing from ChatGPT but it’s not working ??

    add_filter('wpcf7_posted_data', 'customize_email_recipient');
    
    function customize_email_recipient($posted_data) {
        // Check if the form ID matches the form you want to customize
        if ($posted_data['_wpcf7'] == '178378c') { // Replace '178378c' with your form ID
            // Get the value of your custom field
            $custom_email = $posted_data['email'];
    
            // Set the email recipient to the custom email address
            $posted_data['mail']['recipient'] = $custom_email;
        }
        return $posted_data;
    }

    Thread Starter schmicho

    (@schmicho)

    Got help from the WordPress Discord Server ??

    add_action('wpcf7_before_send_mail', 'wpdudecom_modify_recipient_email');
    
    function wpdudecom_modify_recipient_email($contact_form) {
        $target_form_id = *********FORMULAR ID***********;
        if ($contact_form->id() != $target_form_id) {
            return;
        }
    
        $submission = WPCF7_Submission::get_instance();
        if (!$submission) {
            return;
        }
    
        $post_id = $submission->get_meta('container_post_id');
        if (!$post_id) {
            return;
        }
    
        $custom_email = get_field('********ACF FIELD NAME********', $post_id);
        if (!$custom_email || !is_email($custom_email)) {
            return;
        }
    
        $mail_properties = $contact_form->prop('mail');
        $mail_properties['recipient'] = $custom_email;
        $contact_form->set_properties(['mail' => $mail_properties]);
    }
    swbdatw

    (@swbdatw)

    That’s excactly what I need! But doesn’t work for me… ??

    What must be entered in the e-mail recipient field in the CF7 settings?

    Where can I find the “container_post_id” in the source code?

    Thread Starter schmicho

    (@schmicho)

    You can find the FORMULAR ID in the ID of the form.
    It sais xxxx.com/wpadmin/admin.php?page=wpcf7&post=4101&action=edit
    The Post=4101 is the formular ID

    The ACF FIELD NAME however is the Field Name in ACF (not the Field Label)
    Hope it helps.

    swbdatw

    (@swbdatw)

    OK, one mistake was that I had taken the form ID in my case “5a4b224” from the shortcode…

    [contact-form-7 id=”5a4b224″ title=”Contact Form”]

    But the ACF field name is correct… Nevertheless, the emails are only ever sent to the admin of the website! ??
    I don’t know what I’m doing wrong…

    It is also not clear to me where the “container_post_id” comes from. I can’t find one in the source code, maybe it can’t generate the $post_id

    • This reply was modified 10 months ago by swbdatw.
    Thread Starter schmicho

    (@schmicho)

    Can you send the code here ? Maybe I can help

    swbdatw

    (@swbdatw)

    OK. I have inserted this code at the end of the functions.php …

    add_action('wpcf7_before_send_mail', 'modify_recipient_email');
    
    function modify_recipient_email($contact_form) {
        $target_form_id = 506;
        if ($contact_form->id() != $target_form_id) {
            return;
        }
    
        $submission = WPCF7_Submission::get_instance();
        if (!$submission) {
            return;
        }
    
        $post_id = $submission->get_meta('container_post_id');
        if (!$post_id) {
            return;
        }
    
        $custom_email = get_field('form_email', $post_id);
        if (!$custom_email || !is_email($custom_email)) {
            return;
        }
    
        $mail_properties = $contact_form->prop('mail');
        $mail_properties['recipient'] = $custom_email;
        $contact_form->set_properties(['mail' => $mail_properties]);
    }

    In the CF7 e-mail settings, I have inserted the shortcode [_site_admin_email] in the recipient field.

    Thread Starter schmicho

    (@schmicho)

    Did you filled out the ACF Field inside the post ?
    Because the code looks good

    swbdatw

    (@swbdatw)

    Yes, of course and I have set up the AC field as an email field.
    I have to mention that the form is all built in on posts of a custom post type.
    Could that be causing the error?

    Thread Starter schmicho

    (@schmicho)

    maybe. try with a standart post and see if it works

    swbdatw

    (@swbdatw)

    I tried it out… but it still doesn’t work. ??

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Custom Fields as E-Mail reciever’ is closed to new replies.