Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
    
Can you please try to make you question more clear? (1) is_single() tag was positive the first time and negative the second (2) In the home or other pages it is shown only once, etc. — are not clear. Please take some time, and rewrite your question more clearly. :) –  its_me Jun 30 '12 at 12:08
2  
Are you sure that your function is called twice on the same page ? Do you have an ajax call or something similar that could trigger the second call ? You could just use var_dump instead of error_log so that you can see where the code execution takes place. –  Fabien Quatravaux Aug 5 '13 at 14:11
1  
try - error_log( print_r( debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ), TRUE ) ); - to see how the calls are invoked –  Magenta Cuda Apr 6 at 2:03

1 Answer 1

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');
});
share|improve this answer
2  
With PHP 5 it's possible to use anonymous functions, javascript like. So you can pass the function directly in the arguments php.net/manual/en/functions.anonymous.php –  Bakaburg Jun 30 '12 at 19:04
    

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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