Try loading the Javascript into the page using this snippet in functions.php:
add_action( 'wp_enqueue_scripts', 'add_my_script' );
function add_my_script() {
$my_page_id = 57; // set to the required page
if ($my_page_id == get_the_id() ) {
wp_enqueue_script( 'my-js', '/wp-content/uploads/my_script.js', array(), "1", true);
}
}
Remove the “if” to get it to load on all pages.
thanks @lorro !
but i am confused about my_script.js . i don’t have any such file in my file .
Substitute whatever script you want to load into the page. I think you put:
//www.townscript.com/popup-widget/townscript-widget.nocache.js
@lorro can you Explain please!
i can put my js file url but one more problem that is i am unable to save my this code in variation description <button onclick="popup('cspo-pun-170301');" class="tsbutton">Register Now</button><noscript id="tsNoJsMsg">Javascript on your browser is not enabled.</noscript>
my question is that why its not saving above code i variation description
but if i am putting this code on my page then there is no problem then why its not saving in variation description ??
Sure, in your original post you said you had attempted to load a JavaScript into the page with this bit:
<script src="//www.townscript.com/popup-widget/townscript-widget.nocache.js" type="text/javascript"></script>
but the editor had striped it out, leaving only the button. Its not the best place to load a script, which is why the editor removed it. So, that leaves you with the problem of loading the script into the page. To do so, you can try this function:
add_action( 'wp_enqueue_scripts', 'add_my_script' );
function add_my_script() {
$my_page_id = 57; // set to the required page
if ($my_page_id == get_the_id() ) {
wp_enqueue_script( 'my-js', '//www.townscript.com/popup-widget/townscript-widget.nocache.js', array(), "1", true);
}
}
The wp_enqueue_script() function will add a line in your page markup to load the script. Might be in the head section or at the bottom. Anyway, it will be brought into the page. Then the code will be available to execute the call from your button.
The code snippet goes in functions.php. You can put it in functions.php for your theme for testing, but this will be overwritten by theme updates, so its best to make a child theme and put the snippet in functions.php for the child theme.
In the snippet example, the script is only loaded for page id 57. You can get your page id from the url in the dashboard page edit screen, or often its in the body tag in the page markup.
If you want to load the snippet in all pages, remove the “if” line and the corresponding closing bracket “}”.