javascript - WordPress 根本不加载插件脚本

标签 javascript jquery wordpress wordpress-theming custom-wordpress-pages

所以我可以像这样在我的插件中加载脚本。

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/

相关文章:

javascript - React Router 重新渲染路由而不是重新渲染组件

javascript - Table2excel 插件不起作用

html - 有没有办法查看网页上所有 div 标签周围的边框?

css - 如何将 woocommerce 的小部件显示到页面的侧边栏中

javascript - 从选择输入中显示和隐藏多个输入框

javascript - 如何使用按钮 Bootstrap Accordion 更改 (-,+) 符号?

javascript - Stripe : This Element will be mounted to a DOM element that contains child nodes

javascript - 容器持有选框动画的问题 - jquery

javascript - 更改所选选项的颜色(选中时)

javascript - 随机设置背景图片