I need a bit of help for a plugin I'm writing. I need to load a custom stylesheet and a custom script that are needed on the frontend only. Usually I use the wp_enqueue_script
and wp_enqueue_style
with the get_template_directory_uri()
but these scripts are inside the plugin folder and the javascript one needs that swiper.js is loaded and available. On the backend this will cause an error because swiper is not loaded for the backend. How I can fix this and enqueue correctly the scripts?
Here is the code I'm using:
public function __construct()
{
parent::__construct(
'i-widget',
'I feed',
array(
'description' => ''
)
);
add_action( 'widgets_init', array($this, 'init') );
add_action( 'wp_enqueue_scripts', array($this, 'init' ) );
}
public function init()
{
register_sidebar(
array(
'name' => 'I feed',
'id' => 'i-feed',
'description' => 'I feed widget',
)
);
register_widget( 'IFeedWidget' );
wp_enqueue_style('i-widget', plugins_url( 'i-widget.min.css' , __FILE__), array(), null);
wp_enqueue_script('i-widget', plugins_url( 'i-widget.min.js', __FILE__), array('jquery, swiper'), null);
}