I need to run this javascript code on a specific wordpress page.
The code inserted in the “Head”, now works on all pages.
What code should I insert?
<script src="https://example.com/code.js" type="text/javascript"></script>
I am not a developer, but a newbie.
Leonardo Bellotti is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
Because you’re new to save a meltdown. Rather than editing functions.php (1 typo and your whole instance goes down). I suggest you either install one of the many code plugins that allow you to target a specific page. Or just add a code module to the content of the page and put your script in there.
Please use the code below. Add this code to your functions.php file:
function cstm_script_spec_pg() {
if ( is_page(123) || is_page('home') ) {
wp_enqueue_script('custom-js', 'https://example.com/code.js', array(), null, true);
}
}
add_action('wp_enqueue_scripts', 'cstm_script_spec_pg');
Note:
is_page(123): Replace 123 with the ID of the page where you want to run this script.
is_page(‘home’): Replace home with the slug of the page where you want to run this script.
Nidhit34 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.