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

functions - wp_enqueue_scripts called twice?

programmeradmin2浏览0评论

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
  • 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 Commented Jun 30, 2012 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 Commented Aug 5, 2013 at 14:11
  • 1 try - error_log( print_r( debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ), TRUE ) ); - to see how the calls are invoked – user27457 Commented Apr 6, 2014 at 2:03
Add a comment  | 

2 Answers 2

Reset to default 1

I 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');
});
发布评论

评论列表(0)

  1. 暂无评论