2

I tried everything and my action hook wp_enqueue_scripts doesn't work! What can stop it from working? This is my code inside the plugin. The plugin is activated:

function wpb_adding_scripts() {
    error_log("try 9");
}

add_action ('wp_enqueue_scripts', 'wpb_adding_scripts');
  • 1
    This wp_enqueue_scripts is for adding stylesheets or JavaScript files to the system. Why are you using this error_log("try 9"); function here ? – CodeMascot May 23 '17 at 17:39
  • 1
    You are enqueuing for the front and your code is correct, if your trying this for the admin area you will need to use admin_enqueue_scripts. I would deactivate plugins to see if this resolves your issue – Sam May 23 '17 at 17:41
  • 1
    Deactivate your plugins also include a file and then use the console to check for either css files load or JavaScript if this works then start to activate plugins one at a time – Sam May 23 '17 at 17:43
  • 1
    If you right click on a page and select inspect element you will get a console this will have resources loaded like images , css , js and other features you can look in the css folder to see what ones have been included in your page – Sam May 23 '17 at 17:45
  • 2
    @VladimirDespotovic does your theme call the wp_head() function? This is what invokes the wp_enqueue_scripts action. – Milo May 23 '17 at 17:52
3

The wp_enqueue_scripts function is hooked to run on the wp_head action, which is triggered by the wp_head() function.

This function should be placed within the <head> tag of your theme's markup.

If we refer to the core file default-filters.php, we can see the many functions that rely on wp_head for output.

1

The proper way to enqueue your scripts is the following:

function my_scripts(){
    wp_enqueue_script ('my-js','YOUR JS URL HERE');
    wp_enqueue_style ('my-style','YOUR CSS URL HERE');
}
add_action('wp_enqueue_scripts','my_scripts');

You are using the hook that fires when queuing scripts, but not queuing anything. Take a look into Codex to notice the difference between wp_enqueue_script() and wp_enqueue_scripts().

Also, the 5th argument of wp_enqueue_script() allows you to queue your script in the footer. If you have problem with yout wp_head() hook, try the footer:

wp_enqueue_script ('my-js' , 'YOUR JS URL HERE' , '' , '' , true);

This will queue your js file in the footer, which is required when you need the DOM to be ready for the js file to process.

  • yes, I know that I am not yet queueing the script, but I just want to go inside the function, and I am not getting in. – Vladimir Despotovic May 23 '17 at 18:06
  • 1
    @VladimirDespotovic So your function is not fired at all?! That's strange, and huge possibility is some plugin or theme is interfering. – Jack Johansson May 23 '17 at 18:11
  • Do you have any idea how can a theme or plugin interfere with this functionality, since it is basic native wordpress functionality? It can be the case though, since I am working on someone else's theme. – Vladimir Despotovic May 23 '17 at 18:17
  • 1
    @VladimirDespotovic Have you tried the usual disabling of plugins and switching to the default them to see which one is triggering the issue? That would narrow your possibilities by a great deal. – Jack Johansson May 23 '17 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.