WP Enqueue Script for Widget Plugin

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

Read More
/**
 * 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

Related posts

Leave a Reply

1 comment

  1. 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'];
        }