0

I made a template called test-stuff.php and tried using jQuery and the jQuery validation plugin in it. I've enqueued the scripts in my functions.php file and the jQuery works fine on other regular pages, but not in my template. Could someone point out where I went wrong? Do I have to enqueue jquery and the jquery plugin separetely for the template? If I have left out some details, please ask and I will provide. Thanks in advance.

functions.php enqueuing part:

function my_custom_queue() {
    wp_enqueue_script('jquery');
    wp_enqueue_script( 'validatejq', 'https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/jquery.validate.min.js', array( 'jquery' ), '1' );
}
add_action( 'wp_enqueue_scripts', 'my_custom_queue' );

Template file with jquery testing function(I get "no"):

<?php 

/* Template name: Test stuff */

?>
<script>
window.onload = function() {
    /* test */
    if (window.jQuery) {
        alert("Yes");
    } else {
        alert("No");
    }
};

</script>
Sparky
  • 98,165
  • 25
  • 199
  • 285
Zae
  • 47
  • 9
  • 1
    You did not include `get_header()` and `get_footer()` in your template, so the scripts are never included. Or is that one of the details you left out? – Teuniz Mar 06 '19 at 13:07
  • Yes that was the issue, thank you for responding so quickly. Sorry about the weirdly easy problem. – Zae Mar 06 '19 at 13:28
  • 1
    No problem @Zae, happy to help! – Teuniz Mar 06 '19 at 16:48

2 Answers2

1

As far as I know, you don't need to enqueue, it is already enqueue by WordPress. Check some of the already included JS libraries. You have not called get_header() and get_footer() either.

You can check out the following link to get the list of already include libraries.

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

Sagar Bahadur Tamang
  • 2,670
  • 2
  • 24
  • 41
1

You should use get_header() in your page template in order get the header code (including the enqueued scripts) in your page. And you probably want to use get_footer() too. Some scripts are included in the footer.

Teuniz
  • 133
  • 6