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?!
3 Answers
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');