2
votes

I was working on a widget plugin for WordPress the other day and I was having trouble implementing the wp enqueue script function in the code below.

https://codex.wordpress.org/Function_Reference/wp_enqueue_script

/**
 * 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'];
    }
    echo __( '<script type="text/javascript" src="https://d21djfthp4qopy.cloudfront.net/humanitybox.js "></script>', 'text_domain' );
    echo $args['after_widget'];
}

I've been trying for hours, but I can't get it to work. Below is a link to the Github project page.

I'm pretty new to writing plugins for WordPress, so more detail would be extremely helpful.

https://github.com/ModMarc/WordPress-Humanity-Box-Widget/blob/master/Widget.php

1
I'm not seeing any attempt to enqueue the script in your code. I recommend you make an attempt based on the examples in the wp_enqueue_script documentation. Post that code if it doesn't work. - Mathew Tinsley
I know... The code above is the only way I got the plugin to work : ( - Marc Woodyard

1 Answers

1
votes

try this script it's work

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_style('jquery-style', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css');
                wp_enqueue_script('event-default', plugin_dir_url(__FILE__) . '/js/default.js', array('jquery'));
        //echo __( '<script type="text/javascript" src="https://d21djfthp4qopy.cloudfront.net/humanitybox.js "></script>', 'text_domain' );
        echo $args['after_widget'];
    }