cancel
Showing results for 
Search instead for 
Did you mean: 

Add opt in check box to wordpress contact form

Occasional Contributor

Add opt in check box to wordpress contact form

Hi, I have a contact form on my wordpress site and I would like to add an opt in box so that the user can choose to be added to the newsletter subscription if they wish when they send the contact form.

 

<input name="emailUpdates" type="checkbox" value="Yes" /> 
<label>Register for exclusive offers and promotions?</label>

 

I am having problems finding info on how to integrate this.

 

Here is my code. It's a basic wp_mail form.

 

<?php
$validation_message = "";

if( isset($_POST['submit']) ){

$passes = true;
$validation_template = "<div class='alert %s'><i class='fa %s'></i> %s</div>";

// Verify nonce before proceeding
if( !isset( $_POST['sc_contact_form_nonce'] ) || !wp_verify_nonce( $_POST['sc_contact_form_nonce'], basename( __FILE__ ) ) ){
$validation_message = sprintf($validation_template, 'alert-error', 'fa-times-circle', 'Sorry Something went wrong, please try again.');
$passes = false;
}

if( !$_POST['form_fname'] || !$_POST['form_lname'] || !$_POST['form_email'] || !$_POST['form_message'] ){
$validation_message = sprintf($validation_template, 'alert-error', 'fa-times-circle', 'Please fill in all fields.');
$passes = false;
} elseif( !preg_match('/(^.*@.*$)/', $_POST['form_email']) ){
$validation_message = sprintf($validation_template, 'alert-error', 'fa-times-circle', 'Please provide a valid email address.');
$passes = false;
}

if( $passes ) {
//Build Message

$message = 'First Name: '.$_POST['form_fname']."\n";
$message .= 'Last Name: '.$_POST['form_lname']."\n";
$message .= 'Email: '.$_POST['form_email']."\n";
$message .= 'Development of interest: '.$_POST['form_devname']."\n";
$message .= "Message:\n" .$_POST['form_message'];

$validation_message = sprintf($validation_template, 'alert-success', 'fa-check-circle', 'Thank you for your enquiry. We will be in touch as soon as possible.');



//add_filter( 'wp_mail_content_type', 'set_html_content_type' );
$headers = 'From: '.$_POST['form_fname'].' '.$_POST['form_lname'].' <'.$_POST['form_email'].'>' . "\r\n";
wp_mail( "me@myco.co.uk", "Website Enquiry", $message, $headers, $attachments );

}

} else {
$passes = false;
}
?>

<div class="col-sm-12 row">
    
	
<div id="enquiry"></div>

    <form id="contact-form" action="#enquiry" class="row" method="POST" <?php echo $passes ? 'style="display:none;"' : '' ; ?>>
    
    <?php wp_nonce_field( basename( __FILE__ ), 'sc_contact_form_nonce' ); ?>
    
    <div class="form-group col-sm-6">
    <input id="contact_fname" class="form-control" type="text" name="form_fname" required placeholder="First Name" value="<?php echo isset($_POST['form_fname']) ? $_POST['form_fname'] : '' ; ?>" />
    </div>
    
    <div class="form-group col-sm-6">
    <input id="contact_lname" class="form-control" type="text" name="form_lname" required placeholder="Last Name" value="<?php echo isset($_POST['form_lname']) ? $_POST['form_lname'] : '' ; ?>" />
    </div>
    
    <div class="form-group col-sm-12">
    <input id="contact_email" class="form-control" type="text" name="form_email" placeholder="Your Email Address" value="<?php echo isset($_POST['form_email']) ? $_POST['form_email'] : '' ; ?>" />
    </div>
    <input id="contact_devname" class="form-control col-sm-12" type="hidden" name="form_devname" placeholder="" value="<?php single_post_title();?>" />
    <div class="form-group col-sm-12">
        <textarea id="contact_message" class="form-control" name="form_message" placeholder="Your Enquiry" rows="8"><?php echo isset($_POST['form_message']) ? $_POST['form_message'] : '' ; ?></textarea>
    </div>
    
    <div class="form-group col-sm-12">
    
    
    <input name="emailUpdates" type="checkbox" value="Yes" /> 
    <label>Register for exclusive offers and promotions?</label>
    </div>
    
    <div class="col-sm-12">
    <button class="btn" name="submit">Send Enquiry</button>
    </div>
    </form>
</div>
5 REPLIES 5
Honored Contributor

Re: Add opt in check box to wordpress contact form

Hello,

 

It is absolutely possible to do this by adding some code to your PHP that will send the contact to your Constant Contact account by using our embeddable signup form system.

 

To get up and running with this, you'll need to start by creating a form in your account and getting the embed code. When you create the form, be sure to choose the list you wish to place your contacts on. Once you've got the embed code, you'll want to find your CA ID. You can find information about all of this here:

Creating your form: https://knowledgebase.constantcontact.com/articles/KnowledgeBase/18265-Create-a-Sign-Up-Form-with-th...

Get your Embed code and find the CA ID: http://developer.constantcontact.com/docs/signup-forms-tools/signup-form-code.html

 

 Next, you'll want to find the List ID in the embed code. You can find it by looking for this code:

 

 

 <input data-id="list:input" type="hidden" name="list" value="LISTID">

 

 

Once you have both the CA and List ID, you can use this sample PHP code to add contacts to your account:

<?php

$url = "https://visitor2.constantcontact.com/api/signup";
$ca = "YOUR_CA_HERE";
$email = "email@example.com";
$first_name = "First Name";
$last_name = "Last Name";
$lists[] = "LIST_ID_1";
$lists[] = "LIST_ID_2";
$lists[] = "LIST_ID_3";

$post_vars = "ca=" . $ca . "&email=" . $email . "&first_name=" . $first_name . "&last_name=" . $last_name;
foreach ($lists as $list) {
	$post_vars .= "&list=".$list;
}
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_vars);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$success = $http_code == 200 ? true : false;

?>

Once you've got this code successfully creating contacts in your Constant Contact account, it should be pretty straightforward to adapt it into your form. If you have any questions, please let me know!

 

Sincerely,

Elijah G.
API Support Engineer
Frequent Visitor

Re: Add opt in check box to wordpress contact form

This is crazy. We way a lot of money for Constant Contact. Why can't you all add a check box option to the forms? It would be very helpful to many, i'm sure.

Moderator

Re: Add opt in check box to wordpress contact form

Hi @MeredithP9,

 

This post is from 2016 and is related to a customer that created their own form for WordPress. Did you create your own form or are you using the Constant Contact form created by Constant Contact?

 

If you are using the plugin that can be installed from https://wordpress.org/plugins/constant-contact-forms/ then I highly suggest you make a post on the forums under the support section on that page. The developer for that plugin monitors those forums for suggestions and feedback there. I will also be happy to send along your feedback, but those forums get the information to the developer directly.


Regards,
Jimmy D.
Tier II API Support Engineer
Visitor

Re: Add opt in check box to wordpress contact form

Hi, I see that the CA id is no longer used and this method was from 2016. Is there an updated version of this php code or a new way to accomplish this?

 

I'd like to integrate an opt-in checkbox into our existing contact forms on our Wordpress website to capture those email addresses in Constant Contact automatically.

 

Please advise. Thank you!

Moderator

Re: Add opt in check box to wordpress contact form

Hi @JohnF25,

 

The forms provided by Constant Contact work with a single contact list. If you are looking to use multiple contact lists and use a check box system you will either want to use a third party sign-up form, or you will want to create your own sign-up form using our API.

 

For a WordPress site I know there are a few different plugins that work with Constant Contact that are created by third party companies and they offer check box forms however; I do not know the exact names of the plugins.


Regards,
Jimmy D.
Tier II API Support Engineer