Hello - I have a javascript I’d like to add in the of one of my pages.
I’m using X 4.6.3.
How can I do it? Is there a plugin for instance, that is known to work with X?
Kind regards
Rick
Hello - I have a javascript I’d like to add in the of one of my pages.
I’m using X 4.6.3.
How can I do it? Is there a plugin for instance, that is known to work with X?
Kind regards
Rick
Hi Rick,
Try adding the JS code in a Raw Content element.
Also, you can insert the JS code through the functions.php file of the child theme.
add_action( 'wp_head', 'custom_page_script', 10 );
function custom_page_script(){
if ( is_page(123) ) {
?>
<!-- Add you JS code here -->
<?php
}
}
Then change the value 123 to the page ID where you want the script added.
Hope this helps.
Thanks very much, Jade.
If I need to call a CDN javascript, though, can I do that using a) a raw content element or using b) the functions.php?
If so, how would I do that?
The lines I’d like to add in my are:
<script src="/path/to/captcha.js"></script>
<script src="https://www.google.com/recaptcha/api.js?onload=tryInitCaptcha&render=explicit"></script>
Can I just paste them like this:
add_action( 'wp_head', 'custom_page_script', 10 );
function custom_page_script(){
if ( is_page(123) ) {
?>
<!-- Add you JS code here -->
<script src="/path/to/captcha.js"></script>
<script src="https://www.google.com/recaptcha/api.js?onload=tryInitCaptcha&render=explicit"></script>
<?php
}
}
The first is just a file. I’m not even sure where I should store the file. Something like /framework/js under the theme? Or, do I upload it into media?
Thank you for your help.
Kind regards
Rick
Hello There,
You can use this code instead:
add_action( 'wp_head', 'custom_page_script', 10 );
function custom_page_script(){
if ( is_page(123) ) {
?>
<!-- Add you JS code here -->
<script src="<?php echo get_stylesheet_directory_uri(); ?>/framework/js/captcha.js"></script>
<script src="https://www.google.com/recaptcha/api.js?onload=tryInitCaptcha&render=explicit"></script>
<?php
}
}
You will need to upload the JS file in wp-content/themes/x-child/framework/js/. You might need to create the folder path if it does not exist yet.
Hope this helps. Please let us know how it goes.
This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.