Loading...

Hooking into Contact Form 7 on your WordPress installation to manipulate the posted data is nice and straightforwards.

We’re going to use the wpcf7_before_send_mail hook to register a user who fills in the form. It breaks down into two main parts, the html for the contact form and the php function to hook into the process and do all the user stuff.

Firstly the html, which is entered into the ‘Form’ window of the contact form plugin:

<ul class="contact-form">
 	<li><label for="text-569">Name</label>[text* text-569 id:text-569 class:inputs akismet:author "Name"]</li>
 	<li><label for="email-720">Email</label>[email* email-720 id:email-720 class:inputs akismet:author_email "Email Address"]</li>
 	<li><label for="textarea-526">Message</label>[textarea* textarea-526 id:textarea-526 class:input-txt "Message"]</li>
 	<li>[recaptcha][submit id:register-submit class:sbmt-btn "SIGN UP"]</li>
</ul>

We’re creating a simple form with three inputs, a recaptcha and a submit button. Give the form a title and save it and a shortcode will be generated and highlighted in blue. Make a note of the value of the ID field as you will need this in a bit. I went for an ID of 36.

Next up lets look at the php function which hooks into the Contact Form 7 process before the mail is sent. This will need to live either in a plugin or in the functions.php file of your theme.

function wpcf7_register_user($wpcf7){
	$submission = WPCF7_Submission::get_instance();
	if ( $submission ) {
	    $posted_data = $submission->get_posted_data();
	}
	if($wpcf7->id() == 36) {
		$email = sanitize_email($posted_data['email-720']);
		if( null == username_exists( $email ) && null == email_exists( $email )) {
			$args = array(
				'user_login'	=> $email,
				'user_email'	=> $email,
				'user_pass'	=> wp_generate_password(),
				'display_name'	=> $posted_data['text-569'],
			);
			$user_id = wp_insert_user( $args );	
			if(is_wp_error($user_id)){
				$error = true;
				$err_msg = 'There was a problem setting up your account. Please try another method.';	
			}
		} else {
			$error = true;
			$err_msg = 'Email address already registered. Please login instead.';	
		}
	}
	if(isset($error) && $error === true) {	
		$msgs = $wpcf7->prop('messages');
		$msgs['mail_sent_ok'] = $err_msg;
		$wpcf7->set_properties(array('messages' => $msgs));			
		add_filter('wpcf7_skip_mail', 'abort_mail_sending');		
	}	
	return $wpcf7;
}
add_action('wpcf7_before_send_mail','wpcf7_register_user');

function abort_mail_sending($contact_form){
	return true;
}

OK to run through this, we begin by declaring the function and including the $wpcf7 object.
The first thing we do here is load in the submission, and then check it, before retrieving the posted data from the form.
Next up we verify this is the form we want by checking it’s ID against the one we’ve created above, in this case 36.

Now I verify whether the user exists and if not create a variable calld $error and create an error message, which I can use later on. If the user does not exist a new user is created, unless there is a problem in which case another error message is created.

If it all went without a hitch and my new user was added to the database then I want the emails to send as I’ve have setup them up using mail and mail 2. If there was a problem at any stage of the process I need to disrupt that chain of events.
So a quick check for the $error variable is in order and if it is found I grab a copy of the current message setup as per the ‘Messages’ tab of the Contact form 7 plugin.

$msgs = $wpcf7->prop('messages');

Then because as far as the form is concerned the process completed AOK we change the ‘mail_sent_ok’ message to our custom error message.

$msgs['mail_sent_ok'] = $err_msg;

Then we overwrite the existing messages so it displays for the person filling out the form.

$wpcf7->set_properties(array('messages' => $msgs));

Finally we add a filter to prevent the mails sending. Notice the filter is declared out side of the function.

add_filter('wpcf7_skip_mail', 'abort_mail_sending');

And there we have it, using those same tools you can get your Contact Form 7 to do allsorts of excellent stuff!

Latest Posts

Portfolio

Contact

Fill in the form below to send us a message. Your details will not be used for promotional purposes or passed on to any third parties.

 

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close