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

Wordpress add_action of wp_head for javascriptcss files in plugin - Stack Overflow

programmeradmin1浏览0评论

For some reason, the following is not loading my JS and CSS into wordpress. This code is in an activated plugin, which is otherwise executing. Also, I checked and wp_head does exist in the theme.

add_action ( 'wp_head', 'lbIncludes');

function lbIncludes()  {
    wp_enqueue_script('jquery');

    wp_register_script('myScript',
    WP_PLUGIN_URL . '/myScript/myScript.bundle.js' );
    wp_enqueue_script('myScript');

    wp_register_style('myStyle', WP_PLUGIN_URL . '/myScript/myScript.css');
    wp_enqueue_style('myStyle');
};

Bonus question - what's the appropriate way to make the script wait for jquery to load first?

For some reason, the following is not loading my JS and CSS into wordpress. This code is in an activated plugin, which is otherwise executing. Also, I checked and wp_head does exist in the theme.

add_action ( 'wp_head', 'lbIncludes');

function lbIncludes()  {
    wp_enqueue_script('jquery');

    wp_register_script('myScript',
    WP_PLUGIN_URL . '/myScript/myScript.bundle.js' );
    wp_enqueue_script('myScript');

    wp_register_style('myStyle', WP_PLUGIN_URL . '/myScript/myScript.css');
    wp_enqueue_style('myStyle');
};

Bonus question - what's the appropriate way to make the script wait for jquery to load first?

Share Improve this question edited Mar 15, 2013 at 16:12 its_me 11.4k25 gold badges87 silver badges135 bronze badges asked Apr 22, 2010 at 8:35 MatrymMatrym 17.1k35 gold badges99 silver badges141 bronze badges 2
  • Sigh. You can't enqueue on wp_head. Rather, you have to do it on an init action, such as: add_action ( 'init', 'lbIncludes'); But I still can't get the css to appear. – Matrym Commented Apr 22, 2010 at 9:04
  • Could it be the }; in the last line? It should be } – its_me Commented Mar 15, 2013 at 16:09
Add a ment  | 

2 Answers 2

Reset to default 2

If you want to load jQuery before your scripts do this:

wp_register_script('myScript', WP_PLUGIN_URL . '/myScript/myScript.bundle.js', array('jquery') );

Try hooking to wp_print_styles and wp_print_scripts. To ensure jQuery is loaded first add the $deps parameter when you wp_enqueue_script.

发布评论

评论列表(0)

  1. 暂无评论