Hi Guys,

Today I would like to share my experience adding dynamic data on Contact form 7 email template.  The logic here is to create a hidden field and dynamically adding value on to the hidden field using wpcf7_posted_data filter hook.

Step 1: Create a hidden field on contact form 7 form: I had created [hidden dynamic-content] 

dynamic-content-form

Step2: Use contact form 7 wpcf7_posted_data to add dynamic data on that hidden field.

Here on this hook you can get all the submitted value and can also operate with user submitted data. Here is a quick example of how I write code for this. You can add this code to creating the new file or can just use hooks on your theme functions file.

<?php
/**
* This wil add dynamic data to hidden field: dynamic-content
*/
class Cf7_Add_Dynamic_Value {
	/**
	* Holds contact form 7 property
	*/
	protected $contact_form;

	//main constructor
	public function __construct() {
		add_filter( 'wpcf7_posted_data', array( $this, 'add_dynamic_data' ) );
	}

	/**
	* @hooked wpcf7_posted_data
	* @param array $posted_data
	*/
	public function add_dynamic_data( $posted_data ) {
		$quote = '';
		$your_name = isset( $posted_data[ 'your-name' ] ) ? sanitize_text_field( $posted_data[ 'your-name' ]  ) : '';
		$your_email = isset( $posted_data[ 'your-emai' ] ) ? sanitize_text_field( $posted_data[ 'your-emai' ]  ) : '';
		$your_subject = isset( $posted_data[ 'your-subject' ] ) ? sanitize_text_field( $posted_data[ 'your-subject' ]  ) : '';
		//you can fillup this value as per your requirement. Replace dynami-content with the hidden name added on your form
		$posted_data[ 'dynamic-content' ] = 'Please add operation to add on this value'
		return $posted_data; 
	}

	
}

new Cf7_Add_Dynamic_Value();

 

Step 3: Add a hidden field to the email template.

email-body

Finally, user will get “Please add operation to add on this value” on their email .

Note: This is just a simple example to add value on email template programmatically. You can configure this as per your requirement.

Thanks

Advertisements

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: