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

For some reason, the following is not loading my JS and CSS into wordpress. This code is in an activate

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745190556a4615843.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信