[英]How to add js-file to wordpress widget?
I try to add test.js file to my widget in front-end part. 我尝试将test.js文件添加到前端部分的widget中。 But it's nothing happens.
但它没有任何反应。 The file contain alert.
该文件包含警报。 What's wrong with my code?
我的代码出了什么问题?
class PPNDR_new_widget extends WP_Widget{
function __construct(){
$args = array(
'name' => 'new widget',
'description' => 'this is my first widget',
'classname' => 'ppndr-new-widget'
);
parent::__construct('my_first', '', $args);
}
//front-end display of widget
public function widget($args, $instance) {
add_action('wp_enqueue_scripts', 'CounDounJS_enqueue_js');
function CounDounJS_enqueue_js() {
wp_enqueue_script('resize', plugins_url('/assets/js/test.js', __FILE__));
}
return;
}
}
add_action( 'widgets_init', function() {
register_widget('PPNDR_new_widget');
} );
I had a similar problem and google led me here -- What ended up working for me was eliminating the add_action('wp_enqueue_scripts..
and simply enqueuing my scripts and styles in the widget function. 我有一个类似的问题,谷歌带我到这里 - 最终为我工作的是消除
add_action('wp_enqueue_scripts..
并简单地在widget功能中排队我的脚本和样式。
for example: 例如:
//front-end display of widget
public function widget($args, $instance) {
wp_enqueue_script('resize', plugins_url('/assets/js/test.js', __FILE__));
}
There is the problem of not being able to dequeue the script, but I suspect the need for this would be slim. 存在无法使脚本出列的问题,但我怀疑对此的需求会很小。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.