Mailchimp PHP Integration With WordPress Without A Plugin


About The Author
Cabe Nolan is the founder of WP Cover where he shares his insight into WordPress, development, & entrepreneurship. Outside of WPCover, Cabe continues to run a successful WordPress development firm, Bold City Design as well as a few high profile websites, Arrivala, Two Way Resume, Dock Skipper, and a successful outdoors brand, DolfinPack.







Mailchimp PHP Integration With WordPress Without A Plugin

mailchim-wordpress-without-plugin
I love MailChimp.  I’ve worked with a lot of mailing systems and the API Mailchimp has developed is second to none.  Very easy to use.  What I constantly see is WordPress developers installing plugins in order to integrate form submissions with MailChimp.  Is there a problem with that?  Well technically no.  I have definitely done this as well especially since there is a quick and seamless connection with GravityForms.

However, a lot of times the design of my site may be a bit more complex and trying to style a Gravity Form can be difficult.  The easier approach is to actually to create the form how I want it, and then use MailChimp’s simple PHP API library to handle the submission process.  So how do you go about doing this?  Glad you asked, we’ll go through a simple example below:

First, let’s create our basic form with an email field and submit button.  My example looks like the following:

<form role="form" action="" method="post">
    <div class="input-group">
        <input type="text" class="form-control" name="email" placeholder="Enter your email address">
        <span class="input-group-btn">
            <input type="submit" class="btn btn-secondary">Sign Up</button>
        </span>
    </div>
    <input type="hidden" name="action" value="newsletter" />
	<?php wp_nonce_field('verify_newsletter','nonce_newsletter'); ?>
</form>

 

Next, we need to download the Mailchimp’s PHP library from here: https://bitbucket.org/mailchimp/mailchimp-api-php.  We’ll upload this library to our active theme.  For this example, my library will be located at /wp-content/themes/mytheme/mailchimp and the load file at /wp-content/themes/mytheme/mailchimp.php.

Now that we have all that squared away, it’s time to create our form handler.  Here is the code in full:

if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "newsletter" && check_admin_referer( 'verify_newsletter', 'nonce_newsletter' )) {
	$api_key = "mymcapikey";
	$list_id = "mymclistid";
	require_once( dirname( __FILE__ ) . '/Mailchimp.php');
	$Mailchimp = new Mailchimp($api_key);
	$subscriber = $Mailchimp->lists->subscribe($list_id, array('email' => $_POST['email']));
	
	if ( ! empty($subscriber['leid'])) {
	    // Success
	    $subscribe = 1;
	}
}

 

Here we’re including the Mailchimp library we uploaded to our theme, then we are grabbing the email address and adding it to our designated mailchimp list.  Also note the two requirements from Mailchimp of the API key and Mailchimp List ID, both of these items can be found within your Mailchimp account.

Hope this helps eliminate another plugin from your WordPress install as well as offering additional options for styling your custom form.

 


Share Your Thoughts

Leave a Reply

Your email address will not be published.


Related Stuff You Might Like







WordPress News, Tips, & Code Snippets

Join the WP Cover mailing list and get wordpress news, tips, code snippets, security warnings, and more delivered right to your inbox.  We won't flood your inbox, newsletters typically go out every 1-2 weeks unless it involves an important security release.

You have Successfully Subscribed!