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

javascript - wrong enqueue script order in wordpress child theme - Stack Overflow

programmeradmin5浏览0评论

I'm trying to enqueue my script in a child theme in wordpress, but i get it loaded before Jquery even though I've specified Jquery dependency.

The code:

add_action( 'wp_enqueue_scripts', 'child_theme_scripts' );
function child_theme_scripts(){    
    wp_enqueue_script( 'dynamic-menu-script', get_stylesheet_directory_uri() . '/js/dynamic-menu.js', array( 'jquery' ) );
}

no matter what I do (I tried registering first, then specifying my script in the array after jquery, i tried to specify to load the script in the footer, i tried to enqueue JQuery externally) my script loads before query throwing me a "ReferenceError: Can't find variable: JQuery" I've checked JQ is loading properly but after my script.

this thing is driving me crazy, please help me...

I'm trying to enqueue my script in a child theme in wordpress, but i get it loaded before Jquery even though I've specified Jquery dependency.

The code:

add_action( 'wp_enqueue_scripts', 'child_theme_scripts' );
function child_theme_scripts(){    
    wp_enqueue_script( 'dynamic-menu-script', get_stylesheet_directory_uri() . '/js/dynamic-menu.js', array( 'jquery' ) );
}

no matter what I do (I tried registering first, then specifying my script in the array after jquery, i tried to specify to load the script in the footer, i tried to enqueue JQuery externally) my script loads before query throwing me a "ReferenceError: Can't find variable: JQuery" I've checked JQ is loading properly but after my script.

this thing is driving me crazy, please help me...

Share Improve this question asked Jul 23, 2014 at 9:28 user1218631user1218631 2
  • Are you sure that jQuery dependency is not being changed to another handle in your parent theme? – Rob Schmuecker Commented Jul 23, 2014 at 9:43
  • this is a sample enqueue from parent theme wp_enqueue_script( 'theme-methods', get_template_directory_uri() . '/js/methods.js', array( 'jquery' ), '', true ); – user1218631 Commented Jul 23, 2014 at 9:54
Add a ment  | 

2 Answers 2

Reset to default 6

You can improve the order of scripts loading with priority parameter. Default priority is 10, so you may set it to 11.

add_action( 'wp_enqueue_scripts', 'child_theme_scripts', 11 );

Here's a simple helper function that shows you what functions are assigned to the hook you want to use.

function print_filters_for( $hook = '' ) {
  global $wp_filter;
  if( empty( $hook ) || !isset( $wp_filter[$hook] ) )
    return;

  print '<pre>';
  print_r( $wp_filter[$hook] );
  print '</pre>';
}

you can call it anywhere in templates with print_filters_for( 'wp_enqueue_scripts' );

In addition to @sirBlond,
In my case the hook = '' did not work.

I changed it to:

function print_filters_for($hook)
发布评论

评论列表(0)

  1. 暂无评论