David Stockdale's Scrapcode

MailChimp Popup Button

A simple script that you can use to make your MailChimp popup form button-activated rather than spamming every new visitor with the popup.

This script can simply be added to the “header.php” or “footer.php” file of your child theme:

<script async id="mcjs">
	function showPopup() { 
		!function(c,h,i,m,p){
			m=c.createElement(h),
				p=c.getElementsByTagName(h)[0],
				m.async=1,
				m.src=i,
				p.parentNode.insertBefore(m,p)
		}(
			document,
			"script",
			"https://chimpstatic.com/mcjs-connected/js/users/<YOUR MAILCHIMP API KEY HERE>.js"
		);
		//unsetting the cookie
		document.cookie = "MCPopupClosed=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";           
		document.cookie = "MCPopupSubscribed=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
	}
	document.getElementById("show-popup").onclick = function() { showPopup(); }
</script>

Then you can trigger the popup using either a button:

<button id="show-popup" onclick="showPopup()">Click me</button>

Or with a hyperlink such as this:

<p id="show-popup" onclick="showPopup()"><a href="#" onclick="return false;">Join our mailing list</a></p>

If this quick post helped you out please leave a like or a comment below!

Leave a Reply