• Resolved ybfngbdmb

    (@ybfngbdmb)


    Hi everybody,

    I have a contact 7 form, and everything works just great. I want to customise it a little. When I send my form I want to check 1 of the fields that was entered and when it has a certain value I want to change the To address of the mail that will be sent.

    I’ve been looking in all the contact-form-7 plugin files but I cannot find where I should enter my custom code. Don’t worry about it not being clean, it is a temporary solution, a quick and dirty fix is fine.

    Could anybody point me to the right file?

    Thanks in advance!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter ybfngbdmb

    (@ybfngbdmb)

    To clarify I don’t expect anybody to give me a copy and paste solution, I just need help finding what file is responsible for the processing of the tags and sending the email 🙂

    CF7 defines action hooks so that you can extend CF7 rather than modify it. I’d always go that route to avoid having to re-apply my customisation after every update.

    The CF7 action I use for this is called wpcf7_before_send_mail.

    In my application, the form user enters a reference which I then use to look up the email address I need from the database, just before the email is sent.

    To achieve this I have a placeholder To: address in the form, and a wpcf7_before_send_mail action that does the query and replacement.

    Here’s how the action is defined:

    // My CF7 action
    //
    function wpcf7_set_email_address($contact_form) {
       $submission = WPCF7_Submission::get_instance();
    	if ( $submission ) {
    	   $posted_data = $submission->get_posted_data();
               ...
               // 1.  Get reference from submitted form
               // 2.  Use reference to query database for actual email address
               // 3.  Replace placeholder email address with the correct one from DB
               ...
            }
       }
    }
    add_action("wpcf7_before_send_mail", "wpcf7_set_email_address");
    

    See the CF7 doc for how to access what you need inside the action from the $submission and $contact_form objects.

    You have a choice of where to put your action code so that it will be loaded at the right time. Mine is in a very small custom plugin of my own, but you could also put it in the functions.php file of your theme. If you go the theme route, it’s still best to use a child theme, even if this is your only customisation, to avoid re-applying your code after updates.

    Hope this helps.

    • This reply was modified 8 years, 3 months ago by dcurley. Reason: formatting code
    Thread Starter ybfngbdmb

    (@ybfngbdmb)

    Thank you very much dcurley! 🙂

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Customise contact form 7’ is closed to new replies.