I've been working on a plugin that adds a script to the sidebar when activated. So far, I've been able to display the code, but it loads at the bottom of the page and not in the sidebar.

I'm pretty new to developing WordPress plugins, so more detailed instructions would be greatly appreciated.

Here's the link to the code on Github.

 * Front-end display of widget.
 * @see WP_Widget::widget()
 * @param array $args     Widget arguments.
 * @param array $instance Saved values from database.

    public function widget( $args, $instance ) {

    echo $args['before_widget'];
    if ( ! empty( $instance['title'] ) ) {
        echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_title'];

      wp_enqueue_script( 'external-script', 'https://d21djfthp4qopy.cloudfront.net/humanitybox.js', array( 'jquery' ), '1.0.0', true );

     //echo __( 'Hello, World!', 'text_domain' );
     echo $args['after_widget'];

  • Please add your code inside your question :-) Apr 8, 2015 at 19:21

1 Answer 1


That's expected behaviour, since you're calling wp_enqueue_script mid-page, so at that point the <head> ship has sailed, and the only other option is to enqueue in the footer, which is fine.

I didn't spend too much time reading the actual script that you're enqueuing, but it seems like it it's doing some document.write() as soon as it's loaded, which is why you're probably seeing the output in the footer.

To solve your problem I would rearchitect the script a little bit, turn it into a function that accepts an element and then in your widget output call that function:

<div id="my-widget-id"></div>

That way the script will wait for your .js file to load in the footer, and then call its function passing the widget ID, which you can hopefully insert your output into with $.html().

Hope that helps!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.