Home > Software engineering >  Can I embed javascript/jquery file references directly in custom page templates or do I have to use
Can I embed javascript/jquery file references directly in custom page templates or do I have to use

Time:09-18

I am using a theme (child theme) in WordPress but I needed additional customization and made a custom page template. This custom page template has a very different design to the original theme. It works but JQuery and JavaScript would not register on the custom template. They would register on the child theme but not on the custom page template. I eventually got Javascript/JQuery to work on the custom template when I added <?php wp_head(); ?> to the head tag but that pulled code/design from the child theme into my custom page and ruined it. So I got rid of the enqueuing system and just embedded all the Javascript/JQuery directly into the custom page template.

My question is, do I have to follow the recommended enqueue system because directly embedding was incredibly simple and worked wonderfully?

The section of my head tag with the script references in the custom template is as follows:

    <head>
    <script src= 'https://abbasbooks.com.au/wp-content/themes/twentytwenty-child/js/simplifierappend.js?ver=6.0.2'></script>
    <script src= 'https://abbasbooks.com.au/wp-content/themes/twentytwenty-child/js/label_autocomplete.js?ver=6.0.2'></script>
    <script>var labelnames =<?php echo do_shortcode('[getlabelnames]');?></script>
    </head>

CodePudding user response:

Without wp_head() and wp_footer() function, WordPress enqueue function won't work on custom page template. you need those 2 functions called in your template files if you want to go with enqueue functions.

another important thing, If you care about SEO and other default things to work on your page then you must need to use those 2 functions otherwise you can just write static custom HTML in your template and get the job done.

as you mentioned calling wp_head() loads child/main theme and other css and conflicts with your design then you can use wp_dequeue_style to dequeue specific conflicted css files on specific page or template using is_page_template() or is_page() functions.

  • Related