所以我可以像这样在我的插件中加载脚本。
add_action( 'init', array( $this, 'loadAssets' ) );
相应的函数设置如下:
public function loadAssets() {
// add stylesheet to all pages
wp_enqueue_style(
'styles', // handle
plugins_url( 'styles/main.css', __FILE__ ), // src for js
array(),
WP_DEBUG ? time() : $this->version // version, null removes version, false uses wordpress version
);
wp_register_script( 'jquery.ezmark.min', // handle
plugins_url( 'js/jquery.ezmark.min.js', __FILE__ ), // src for js
array( 'jquery' ), // dependancies
WP_DEBUG ? null : $this->version, // version, null removes version, false uses wordpress version
true
);
}
现在 main.css 加载得很好,但无论我做什么,Javascript 文件根本不会加载。
他们昨天工作得很好,现在当我加载网站时他们停止工作了。
任何帮助将不胜感激。
最佳答案
要在您的插件中包含 CSS 和 jQuery 很简单,请尝试以下操作:
// register jquery and style on initialization
add_action('init', 'register_script');
function register_script() {
wp_register_script( 'custom_jquery', plugins_url('/js/custom-jquery.js', __FILE__), array('jquery'), '2.5.1' );
wp_register_style( 'new_style', plugins_url('/css/new-style.css', __FILE__), false, '1.0.0', 'all');
}
// use the registered jquery and style above
add_action('wp_enqueue_scripts', 'enqueue_style');
function enqueue_style(){
wp_enqueue_script('custom_jquery');
wp_enqueue_style( 'new_style' );
}
关于javascript - WordPress 根本不加载插件脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44455546/