Home » Php » WordPress not queuing scripts

WordPress not queuing scripts

Posted by: admin October 26, 2017 Leave a comment

Questions:

I’m building a new theme and right now things are pretty simple. I’ve got the index.php, style.css, and functions.php files. I’m attempting to load the css and javascript files necessary for the theme. Currently my functions.php file looks like this…

<?php

function get_pds_resources() {
  wp_enqueue_style("main", get_template_directory_uri() . "/assets/css/main.css");
  wp_enqueue_script("jquery", get_template_directory_uri() . "/assets/js/jquery.js");
  wp_enqueue_script("dropotron", get_template_directory_uri() . "/assets/js/dropotron.js");
  wp_enqueue_script("selectorr", get_template_directory_uri() . "/assets/js/selectorr.js");
  wp_enqueue_script("scrollex", get_template_directory_uri() . "/assets/js/scrollex.js");
  wp_enqueue_script("scrolly", get_template_directory_uri() . "/assets/js/scrolly.js");
  wp_enqueue_script("skel", get_template_directory_uri() . "/assets/js/skel.js");
  wp_enqueue_script("util", get_template_directory_uri() . "/assets/js/util.js");
  wp_enqueue_script("main", get_template_directory_uri() . "/assets/js/main.js");
}

add_action("wp_enqueue_scripts", "get_pds_resources");

Problem is that doesn’t seem to load anything at all. No style sheet and no javascript.

Just to make sure my resources can be reached, I put the following in my index.php file and it worked fine.

<link rel="stylesheet" href="<?php echo get_template_directory_uri() . "/assets/css/main.css"; ?>" />
...
...
...
<script src="<?php echo get_template_directory_uri() . "/assets/js/jquery.js"; ?>"></script>
            <script src="<?php echo get_template_directory_uri() . "/assets/js/dropotron.js"; ?>"></script>
            <script src="<?php echo get_template_directory_uri() . "/assets/js/selectorr.js"; ?>"></script>
            <script src="<?php echo get_template_directory_uri() . "/assets/js/scrollex.js"; ?>"></script>
            <script src="<?php echo get_template_directory_uri() . "/assets/js/scrolly.js"; ?>"></script>
            <script src="<?php echo get_template_directory_uri() . "/assets/js/skel.js"; ?>"></script>
            <script src="<?php echo get_template_directory_uri() . "/assets/js/util.js"; ?>"></script>
            <script src="<?php echo get_template_directory_uri() . "/assets/js/main.js"; ?>"></script>

I can do it this way but I’d prefer to do it the correct way if I can get it to work. Anyone have any idea why it isn’t loading anything?

Answers:

i think you are missing the parameters in wp_enqueue_script function

try add these 3 parameters at end of wp_enqueue_script function as in following example

wp_enqueue_script('custom-js',get_template_directory_uri() . '/assets/js/cat.js',array(), null, true);

Questions:
Answers:

You are trying to register handles with names like jquery, main, util. These are probably already in use, jquery will be for sure.

Either use unique handles or use wp_deregister_script() if you want to replace an already registered handle

I’m assuming you are calling wp_head() and wp_footer() somewhere within your theme, if not then it will never work.

Questions:
Answers:

In header section just opening body tag call this hooks…

<?php wp_head()?> 

Questions:
Answers:

Try to prefix handle names, Some of the handle names you are using like main, util, jquery are already declared and used by wordpress core itself, it will create name conflict, either prefix them or use a different handle name.

Questions:
Answers:

use this code in your functions.php of activated theme

function get_pds_resources() {
  wp_enqueue_style("main", get_template_directory_uri() . "/assets/css/main.css");
  wp_deregister_script('jquery');
  wp_enqueue_script("jquery", get_template_directory_uri() . "/assets/js/jquery.js");
  wp_enqueue_script("dropotron", get_template_directory_uri() . "/assets/js/dropotron.js", array('jquery'));
  wp_enqueue_script("selectorr", get_template_directory_uri() . "/assets/js/selectorr.js", array('jquery'));
  wp_enqueue_script("scrollex", get_template_directory_uri() . "/assets/js/scrollex.js", array('jquery'));
  wp_enqueue_script("scrolly", get_template_directory_uri() . "/assets/js/scrolly.js", array('jquery'));
  wp_enqueue_script("skel", get_template_directory_uri() . "/assets/js/skel.js", array('jquery'));
  wp_enqueue_script("util", get_template_directory_uri() . "/assets/js/util.js", array('jquery'));
  wp_enqueue_script("main", get_template_directory_uri() . "/assets/js/main.js", array('jquery'));
}

add_action("wp_enqueue_scripts", "get_pds_resources",11);

hope it will help you.