最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - wp_enqueue_scripts hook is not being called

programmeradmin0浏览0评论

I added the following code to my functions.php:

if(!function_exists('bi_frontend_scripts')) {
    function bi_frontend_scripts() {
        wp_enqueue_script('jquery');
        // I originally wanted to do:
        wp_enqueue_script('jQuery.bxSlider', get_bloginfo('template_url').'/scripts/jquery.bxslider/jquery.bxslider.min.js', array('jquery'));
    }
}
add_action('wp_enqueue_scripts', 'bi_frontend_scripts');

But apparently neither of the scripts is being enqueued. I didn't get what my problem was, so I added this script to find out if the hook is called:

function aal_handler() {
    static $list = array();
    // exclude certain actions:
    $exclude = array('gettext', 'gettext_with_context');

    $action = current_filter();

    if(!in_array($action, $exclude)) {
        $list[] = $action;
    }
    // shutdown is the last action
    if ('shutdown' == $action) {
        print '<pre>' . implode( "\n", $list ) . '</pre>';
    }
}
add_action('all', 'aal_handler', 99999, 99);

And in the list wp_enqueue_scripts does not appear. So why the hook is not called?!

I added the following code to my functions.php:

if(!function_exists('bi_frontend_scripts')) {
    function bi_frontend_scripts() {
        wp_enqueue_script('jquery');
        // I originally wanted to do:
        wp_enqueue_script('jQuery.bxSlider', get_bloginfo('template_url').'/scripts/jquery.bxslider/jquery.bxslider.min.js', array('jquery'));
    }
}
add_action('wp_enqueue_scripts', 'bi_frontend_scripts');

But apparently neither of the scripts is being enqueued. I didn't get what my problem was, so I added this script to find out if the hook is called:

function aal_handler() {
    static $list = array();
    // exclude certain actions:
    $exclude = array('gettext', 'gettext_with_context');

    $action = current_filter();

    if(!in_array($action, $exclude)) {
        $list[] = $action;
    }
    // shutdown is the last action
    if ('shutdown' == $action) {
        print '<pre>' . implode( "\n", $list ) . '</pre>';
    }
}
add_action('all', 'aal_handler', 99999, 99);

And in the list wp_enqueue_scripts does not appear. So why the hook is not called?!

Share Improve this question asked Mar 16, 2014 at 15:07 Julian F. WeinertJulian F. Weinert 6002 gold badges5 silver badges18 bronze badges 2
  • Maybe bi_frontend_scripts() was defined previously and therefore doesn't pass the check for its non-existence? – kaiser Commented Mar 16, 2014 at 15:15
  • No, I already took this into account – Julian F. Weinert Commented Mar 16, 2014 at 15:16
Add a comment  | 

3 Answers 3

Reset to default 5

Your code is correct, though I would not add the callback if the function name has already been used. If someone else has used the name you don't know what you might be adding.

if(!function_exists('bi_frontend_scripts')) {
    function bi_frontend_scripts() {
        wp_enqueue_script('jquery');
        // I originally wanted to do:
        wp_enqueue_script('jQuery.bxSlider', get_bloginfo('template_url').'/scripts/jquery.bxslider/jquery.bxslider.min.js', array('jquery'));
    }
    add_action('wp_enqueue_scripts', 'bi_frontend_scripts');
}

I also tested your "hook dump" function-- aal_handler()-- and that works. wp_enqueue_scripts is part of the output.

I have to conclude that there is something wrong with the theme. wp_enqueue_scripts is hooked to wp_head, which fires in the wp_head() function. The most obvious possibility is that your theme is not using wp_head() correctly, or at all. Another distant possibility is that something has removed your callback-- for example, with:

remove_all_filters('wp_enqueue_scripts');

For anyone getting this problem in an admin page, use the admin_enqueue_scripts hook instead of the wp_enqueue_scripts hook.

Documentation here.

I am here again with a lot of information for you so that you understand how things work with wp_enqueue_scripts.

You need to know the priority order for the wp_enqueue_scripts. See hook order below:

  1. muplugins_loaded
  2. registered_taxonomy
  3. registered_post_type
  4. plugins_loaded
  5. sanitize_comment_cookies
  6. setup_theme
  7. load_textdomain
  8. after_setup_theme
  9. auth_cookie_malformed
  10. auth_cookie_valid
  11. set_current_user
  12. init
  13. widgets_init
  14. register_sidebar
  15. wp_register_sidebar_widget
  16. wp_default_scripts
  17. wp_default_stypes
  18. admin_bar_init
  19. add_admin_bar_menus
  20. wp_loaded
  21. parse_request
  22. send_headers
  23. parse_query
  24. pre_get_posts
  25. posts_selection
  26. wp
  27. template_redirect
  28. get_header
  29. wp_head
  30. wp_enqueue_scripts
  31. wp_print_styles
  32. wp_print_scripts

...and so on.

So you see you can add it in the init hook and doing so it will be called as high priority than below the listing.

If your files are not loading you could use a higher priority hook.

If your wp_enqueue_script is not being called, you can use this simple trick to get it working for your theme or plugin.

<?php

if(!function_exists('bi_frontend_scripts')) {
    function bi_frontend_scripts() {
        wp_enqueue_script('jquery');
        // I originally wanted to do:
        wp_enqueue_script('jQuery.bxSlider', get_bloginfo('template_url').'/scripts/jquery.bxslider/jquery.bxslider.min.js', array('jquery'));
    }

    add_action( 'init', 'bi_frontend_scripts' );
}


?>
发布评论

评论列表(0)

  1. 暂无评论