Trying to add a JS snippet before closing </head> tag - Stuck!

Hi All

I am using an application called Mailer Lite which is basically Mail Chimp but better for my needs. They allow a Pop-Up with an email signup form when you click a button with specific link attached.

I need to add a JavaScript Snippet to my site to allow this to work and they advise it needs to be added right before the closing tag. I have trawled through the forums and tried a number of things but nothing seems to work.

I have activated my sites Child Theme and tried to add the JS snippet to the functions.php file but this bricks the site (simply turns white and nothing loads).

Is someone able to help me please?

Any help would be really gratefully received.

Best wishes

Alex

This is the JS snippet I need to add:

 <!-- MailerLite Universal -->
<script>
(function(m,a,i,l,e,r){ m['MailerLiteObject']=e;function f(){
var c={ a:arguments,q:[]};var r=this.push(c);return "number"!=typeof r?r:f.bind(c.q);}
f.q=f.q||[];m[e]=m[e]||f.bind(f.q);m[e].q=m[e].q||f.q;r=a.createElement(i);
var _=a.getElementsByTagName(i)[0];r.async=1;r.src=l+'?v'+(~~(new Date().getTime()/1000000));
_.parentNode.insertBefore(r,_);})(window, document, 'script', 'https://static.mailerlite.com/js/universal.js', 'ml');

var ml_account = ml('accounts', '1176522', 'm5j7x3l3w4', 'load');
</script>
<!-- End MailerLite Universal -->

Hello There,

Thanks for writing in!

Please be advised that only php codes is allowed to be inserted in the child theme’s functions.php file. This is why it turns to white screen when you have inserted the JS code. You must do it the proper way. To resolve your issue, please add the following code in your child theme’s functions.php file

function add_custom_mailerlite(){ ?>
  	<!-- MailerLite Universal -->
	<script>
	(function(m,a,i,l,e,r){ m['MailerLiteObject']=e;function f(){
	var c={ a:arguments,q:[]};var r=this.push(c);return "number"!=typeof r?r:f.bind(c.q);}
	f.q=f.q||[];m[e]=m[e]||f.bind(f.q);m[e].q=m[e].q||f.q;r=a.createElement(i);
	var _=a.getElementsByTagName(i)[0];r.async=1;r.src=l+'?v'+(~~(new Date().getTime()/1000000));
	_.parentNode.insertBefore(r,_);})(window, document, 'script', 'https://static.mailerlite.com/js/universal.js', 'ml');

	var ml_account = ml('accounts', '1176522', 'm5j7x3l3w4', 'load');
	</script>
	<!-- End MailerLite Universal -->
<?php }
add_action('wp_head', 'add_custom_mailerlite');
// =============================================================================

We would loved to know if this has work for you. Thank you.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.