Forum Replies Created

Viewing 4 replies - 1 through 4 (of 4 total)
  • 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

    That’s what I found too @stajan. I don’t think it always worked like that but I could be wrong.

    In case it helps anyone I have a workaround for now but it does mean exposing user details (either login name or ID).

    Workaround is to add a hidden field to the form, which defaults to the user ID:

    [hidden current_user_id "user_id"]

    CF7 fills this in automatically (see notes in the built-in tag generator).

    Then in my wpcf7_before_send_mail() action I pull it out with:

       ...
       $posted_data = $submission->get_posted_data();
       $user_id = $posted_data['current_user_id'];

    From there I can do what my action is for (adding a user_meta item):

    add_user_meta($user_id, 'my_meta_key', 'my_meta_value');

    With the user ID you should be able to do whatever else you need.

    Putting user IDs in the page isn’t great practice, so this is a workaround, not a complete solution.

    Same here. WordPress 4.8.1 and CF7 also 4.8.1. Mine is an infrequently-used form that did work and now doesn’t, but haven’t pinpointed when yet. I’ll post here if I find something useful.

    Add this code to your theme’s functions.php
    Replace ‘your-date’ with the name you gave your date field.

    
    add_filter( 'wpcf7_posted_data', 'your_wpcf7_posted_data' );
    function your_wpcf7_posted_data( $posted_data ) {
    	$date_fields = array( 'your-date' );
    
    	foreach ( $date_fields as $field )
    		$posted_data[$field] = strftime('%e %B %Y', strtotime($posted_data[$field]));
    
    	return $posted_data;
    }
    
    • This reply was modified 9 years, 8 months ago by dcurley. Reason: forgot to format code
Viewing 4 replies - 1 through 4 (of 4 total)