Adding a script to functions.php

Hello! :slight_smile:

I need to add the following script:

<script src="https://widget.newlooknow.com/static/js/script.js?afid=198415" type="text/javascript"></script>

to a specific section on the home page of my site. I have a child theme installed.

Can you guide me?

Thx lovely support people.

Hey there,

Thanks for writing in! If you need to add the above script on homepage then try adding the following code in your child theme’s functions.php file:

add_action('wp_head','custom_script');

function custom_script() {

	if( is_front_page() || is_home() ) {
?>
		<script src="https://widget.newlooknow.com/static/js/script.js?afid=198415" type="text/javascript"></script>
<?php 
	}
}

Let us know how this goes!

Worked perfectly, thanks. If I want to add it to another page, what part of that code do I change? :slight_smile:

HI there,

You will have to change the condition if the if statement in:

if( is_front_page() || is_home() )

You may use the is_page method to target a specific page:

https://developer.wordpress.org/reference/functions/is_page/

Hope this helps.

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