The Mailchimp popup fix (WordPress)

Seeing errors on your WordPress site after installing the MailChimp WordPress popup widget?

The script you paste in is along these lines:

<script type="text/javascript" src="//s3.amazonaws.com/downloads.mailchimp.com/js/signup-forms/popup/embed.js" data-dojo-config="usePlainJson: true, isDebug: false"></script>
<script type="text/javascript">
require(["mojo/signup-forms/Loader"], function(L) { L.start({"baseUrl":"mc.us5.list-manage.com","uuid":"(your uuid)","lid":"(your lid)"}) })
</script>

and it’s busted. It’ll throw errors requiring jQuery in the root directory of your website.

Put jQuery there and guess what! It’ll overwrite all other jQuery bindings on the page, breaking everything else.

C’mon MailChimp, this sucks.

Drop this nice fix in and you’re on your way. Hat tip to @nickcernis (https://gist.github.com/nickcernis/3df74c0c6c18b75cb951)


add_action('wp_footer', 'add_mailchimp_popout');
function add_mailchimp_popout(){
	?>
	
	<script>
    // Fill in your MailChimp popup settings below.
    // These can be found in the original popup script from MailChimp.
    var mailchimpConfig = {
        baseUrl: 'mc.us5.list-manage.com',
        uuid: '(your uuid)',
        lid: '(your lid)'
    };

    // No edits below this line are required
    var chimpPopupLoader = document.createElement("script");
    chimpPopupLoader.src = '//s3.amazonaws.com/downloads.mailchimp.com/js/signup-forms/popup/embed.js';
    chimpPopupLoader.setAttribute('data-dojo-config', 'usePlainJson: true, isDebug: false');

    var chimpPopup = document.createElement("script");
    chimpPopup.appendChild(document.createTextNode('require(["mojo/signup-forms/Loader"], function (L) { L.start({"baseUrl": "' +  mailchimpConfig.baseUrl + '", "uuid": "' + mailchimpConfig.uuid + '", "lid": "' + mailchimpConfig.lid + '"})});'));

    jQuery(function ($) {
        document.body.appendChild(chimpPopupLoader);

        $(window).load(function () {
            document.body.appendChild(chimpPopup);
        });

    });
</script>
Published