By the time your widget (or sidebar if this is hard-coded) loads you have already missed the wp_enqueue_scripts
hook, which runs in the header and that means that you have missed the window for registering the script in the <head>
at all, which you are trying to do– there is no fifth parameter that would load the script in the footer.
You can enqueue in wp_footer
or any other hook that runs after wp_enqueue_scripts
so long as you add the fifth parameter so that the script loads in the footer.
class MyPlugin
{
public function __construct()
{
add_action('wp_footer', array($this, 'enqueueAssets'));
}
public function enqueueAssets()
{
wp_register_script('myplugin', plugins_url('myplugin.js', __FILE__), array('jquery'), '', true);
wp_enqueue_script('myplugin');
}
// More methods
}
While the Codex does state that you should register on wp_enqueue_scripts
, it also says:
As of Version 3.3, the function can be called mid-page (before the
wp_footer() template tag) or using the wp_head action hook. This will
place the script in the footer as if the $in_footer parameter was set
to true.http://codex.wordpress.org/Function_Reference/wp_enqueue_script