I had some problem with WP recognizing a is_single condition for loading scripts called in function.php.
I put a error_log(is_single()) to check what was going on.
When opening a post page the log showed me that log function has been called and the is_sigle() tag was positive the first time and negative the second!
In the home or other pages it is shown only once.
What's going on?
Here's the code in function.php. I'm using a child theme.
add_action('wp_enqueue_scripts', function(){
error_log(is_single());
if (is_single()){
wp_enqueue_script('jquery_transform', get_stylesheet_directory_uri()."/js/jquery.transform.js", array('jquery'),'0.9.3',true);
wp_enqueue_script('zs_image_rotate', get_stylesheet_directory_uri()."/js/image_rotate.js", array('jquery_transform'),'',true);
}
wp_deregister_script('thickbox');
});
I had some problem with WP recognizing a is_single condition for loading scripts called in function.php.
I put a error_log(is_single()) to check what was going on.
When opening a post page the log showed me that log function has been called and the is_sigle() tag was positive the first time and negative the second!
In the home or other pages it is shown only once.
What's going on?
Here's the code in function.php. I'm using a child theme.
add_action('wp_enqueue_scripts', function(){
error_log(is_single());
if (is_single()){
wp_enqueue_script('jquery_transform', get_stylesheet_directory_uri()."/js/jquery.transform.js", array('jquery'),'0.9.3',true);
wp_enqueue_script('zs_image_rotate', get_stylesheet_directory_uri()."/js/image_rotate.js", array('jquery_transform'),'',true);
}
wp_deregister_script('thickbox');
});
Share
Improve this question
asked Jun 30, 2012 at 1:27
BakaburgBakaburg
4733 gold badges8 silver badges21 bronze badges
3
|
2 Answers
Reset to default 1I had this same issue. I logged the request URI and discovered that the additional call was for a file that didn't exist (that's what I get for copying and pasting a bunch of code from one project to another). I created a file at the expected name and location, and the additional call to my wp_enqueue_scripts hook function went away.
So, it seems like one could conclude that a 404 causes a new full request that goes through the full chain of setup functions.
I suspect that your function isn't being run at all, at least in the way you intend.
As per the codex:
$function_to_add (callback) (required) The name of the function you wish to be called. Note: Only string-formatted syntaxes listed in the PHP documentation for the 'callback' type are valid. [Emphasis added]
In your snippet, you place the function directly in the add action call. I can't really explain what's going on with is single, but I'm surprised that the code isn't just breaking completely. Try this:
add_action( 'wp_enqueue_scripts', 'wpse57003_is_single_scripts' );
function wpse57003_is_single_scripts(){
if (is_single()){
wp_enqueue_script('jquery_transform', get_stylesheet_directory_uri()."/js/jquery.transform.js", array('jquery'),'0.9.3',true);
wp_enqueue_script('zs_image_rotate', get_stylesheet_directory_uri()."/js/image_rotate.js", array('jquery_transform'),'',true);
}
wp_deregister_script('thickbox');
});
var_dump
instead oferror_log
so that you can see where the code execution takes place. – Fabien Quatravaux Commented Aug 5, 2013 at 14:11