Support » Plugin: Contact Form 7 » Contact form 7 vs Custom Fields

  • hey im working on school project and each page /post has different responder this mean contact form must be flexible….
    normally each email directly goes to the email address site admin or what we filled up in form settings
    but I like to add custom field and change the responder automatically in each page / post

    /////////////
    example
    jessie post a article about frogs
    but her friend Tom need to access her and ask the question over the contact form
    so
    To : <?php get_field(‘Current_email’); ?>
    name : Tom xxx
    Question : xxxx xxxx xxxx
    ////////////

    how can I do this ?

    The page I need help with: [log in to see the link]

Viewing 15 replies - 1 through 15 (of 16 total)
  • add_filter('wpcf7_before_send_mail', 'myFunction');
    function myFunction($form){
      $posted_data = \WPCF7_Submission::get_instance()->get_posted_data().
    // do something
    }

    Inside this function you can use wp_mail or change mail recipient.

    pls don’t be upset but I don’t understand 🙁

    If email is entered into form, then in form mail settings fill To field with tag, like [Current_email]

    if I say still I don’t understand… probably this time you will beat me with baseball bat….
    1)
    ill add this line to the function.php right ?
    2)
    I don’t understand the part //do something

    this is my code I have to insert the TO: side but still my little brain cannot figure out how so technically im adding short code to adding my short code in template… can I just remove “TO” function in form side and inject this to there ?

    <?php if( the_field('team_post') ): ?>
    <?php get_field('author_mail'); ?>,<?php get_field('team_member_mail'); ?>,<?php get_field('admin_mail'); ?>
    <?php else : ?>
    <?php get_field('author_mail'); ?>,<?php get_field('admin_mail'); ?>
    <?php endif; ?>

    Please explain where do you keep the sender email.
    If it is stored for post with Advanced custom fields, you could do following:

    put this into your functions.php:

    add_filter('wpcf7_before_send_mail', 'myFunction');
    function myFunction($form)
    {
        // get post id from where the form was submitted
        $post_id = $_POST['_wpcf7_container_post'];
    
        // get the email from post field (modify it to actual field value)
        $new_email = get_field('author_mail', $post_id)
        
        // get form properties
        $mailProp = $form->get_properties();
    
        // set the email address to recipient
        $mailProp['mail']['recipient'] = $new_email;
    
        // update the form properties
        $form->set_properties(array('mail' => $mailProp['mail']));
    }

    yes stored in post with advanced custom fields and its wordpress functions.php or wpc7 functions.php ?

    and also can I use like that ?

    // get the email from post field (modify it to actual field value)
        $new_email = get_field('author_mail', $post_id) , get_field('admin_mail', $post_id) , get_field('team_member_mail', $post_id)

    or its limited only 1 recipient ?

    It should be stored in child theme functions.php.

    Or just create /wp-content/mu-plugins folder and put a php file (functions.php or any other name) containing function inside. It will be must-use plugin.

    thank you but gives me syntax error
    something wrong here

    // get form properties
        $mailProp = $form->get_properties();

    Please post here the full text of the error.

    Also this would be better:

    add_filter('wpcf7_before_send_mail', 'myFunction');
    function myFunction($form)
    {
        // get post id from where the form was submitted
        $post_id = $_POST['_wpcf7_container_post'];
    
        // get the email from post field (modify it to actual field value)
        $new_email = get_field('author_mail', $post_id)
        
        if ($new_email){
    // get form properties
        $mailProp = $form->get_properties();
    
        // set the email address to recipient
        $mailProp['mail']['recipient'] = $new_email;
    
        // update the form properties
        $form->set_properties(array('mail' => $mailProp['mail']));}
    }

    its white page on local host but im using Dreamweaver its says sytanx error again
    now this line

       if ($new_email) {
    

    Right, it’s ; missing.

    add_filter('wpcf7_before_send_mail', 'myFunction');
    function myFunction($form)
    {
        // get post id from where the form was submitted
        $post_id = $_POST['_wpcf7_container_post'];
    
        // get the email from post field (modify it to actual field value)
        $new_email = get_field('author_mail', $post_id);
        
        if ($new_email){
    // get form properties
        $mailProp = $form->get_properties();
    
        // set the email address to recipient
        $mailProp['mail']['recipient'] = $new_email;
    
        // update the form properties
        $form->set_properties(array('mail' => $mailProp['mail']));}
    }

    now its working perfectly but still im struggling about more than 1 person

    I had Author , Team Member and admin
    so

    if its team work

    get_field('author_email', $post_id);
    get_field('team_member_email', $post_id);
    get_field('admin_email', $post_id);

    and if its single person project

    get_field('author_email', $post_id);
    get_field('admin_email', $post_id);

    I thing im figure it out but im struggling about coma “,” how can I separate the mail addresses

    there is the situation im in now

    <?
    add_filter('wpcf7_before_send_mail', 'myFunction');
    function myFunction($form)
    {
        // get post id from where the form was submitted
        $post_id = $_POST['_wpcf7_container_post'];
    
        // get the email from post field (modify it to actual field value)
    	//field function start 
    if( get_field('team_work') ):
    	// team project 3 mail
    	$field_names = array('author_email','team_member_email','admin_email');
    	$fields = array();
    	foreach ($field_names as $team_work)
        $new_email = get_field($team_work, $post_id);
    else :    
    	// single project 2 email
    	$field_names = array('author_email','admin_email');
    	$fields = array();
    	foreach ($field_names as $single_work)
    	$new_email = get_field($single_work, $post_id);
    endif ;	
    	//field function end
    
        if ($new_email){
    // get form properties
        $mailProp = $form->get_properties();
    
        // set the email address to recipient
        $mailProp['mail']['recipient'] = $new_email;
    
        // update the form properties
        $form->set_properties(array('mail' => $mailProp['mail']));}
    }
    ?>

    Try this:

    <?php
    // Register a function to 'wpcf7_before_send_mail' hook.
    // It will be called before mail send if form is validated successfully.
    add_filter('wpcf7_before_send_mail', 'myFunction');
    function myFunction($form)
    {
        // get post id from where the form was submitted
        $post_id = $_POST['_wpcf7_container_post'];
    
        $emailList = [];
    
        // get the emails from post fields
        $field_names = ['author_email', 'team_member_email', 'admin_email'];
        foreach ($field_names as $field_name) {
            // Get field value
            $email = get_field($field_name, $post_id);
    
            // Add email to list if field value is not empty
            if (!empty($email)) {
                $emailList[] = $email;
            }
        }
    
        if (!empty($emailList)) {
            // get form properties
            $mailProp = $form->get_properties();
    
            // get the first email and delete it from array
            $first_email = array_shift($emailList);
            // set the email address to recipient
            $mailProp['mail']['recipient'] = $first_email;
    
            // Specify additional Mail headers Cc, i.e. copy
            // Information: https://contactform7.com/adding-cc-bcc-and-other-mail-headers/
            foreach ($emailList as $email) {
                $mailProp['mail']['additional_headers'] .= "Cc: <$email>\n";
            }
    
            // update the form properties
            $form->set_properties(array('mail' => $mailProp['mail']));
        }
    }
    
    // closing php tag should not be used in plain php file

    like a charm 🙂 thank you for your passion and help

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Contact form 7 vs Custom Fields’ is closed to new replies.