• I have function like this, but I dont now how I can get post id… Can you help me?
    Contact form I have in modal popup.

    function cf7dynamicnotifications($contact_form)
    {
    	$submission = WPCF7_Submission::get_instance(); 
    
    	$post_id = $submission->get_meta('container_post_id');
    
    	$newmail = get_field('email', $post_id);
    
       	$mailProp = $contact_form->get_properties('mail');
       	$mailProp['mail']['recipient'] = $newmail;
    
       	$contact_form->set_properties(array('mail' => $mailProp['mail']));
    
    }
    
    add_action( 'wpcf7_before_send_mail', 'cf7dynamicnotifications');
    • This topic was modified 3 years, 5 months ago by mariuszm.
Viewing 1 replies (of 1 total)
  • Hi @mariuszm

    Very interesting question.

    Try this.

    In your functions.php:

    add_action( 'wpcf7_before_send_mail', 'cf7dynamicnotifications' );
    function cf7dynamicnotifications( $contact_form ) {
        $wpcf = WPCF7_Submission::get_instance();
        $fields = $wpcf->get_posted_data();
        $post_id = $fields[ 'post-id' ];
    
        // do something
    
        return $contact_form;
    }
    
    add_action( 'wpcf7_init', 'wpcf7_new_form_tag' );
    function wpcf7_new_form_tag() {
    	wpcf7_add_form_tag( 'post_id', function ( $tag ) {
    		return '<input type="hidden" value="'.get_the_id().'" name="post-id">';
    	} );
    }

    In your CF7 form:

    <label> Your name
        [text* your-name] </label>
    
    <label> Your email
        [email* your-email] </label>
    
    <label> Subject
        [text* your-subject] </label>
    
    <label> Your message (optional)
        [textarea your-message] </label>
    
    [post_id]
    
    [submit "Submit"]
Viewing 1 replies (of 1 total)
  • The topic ‘How get post id?’ is closed to new replies.