【问题标题】:Include js and css in wordpress plugin [duplicate]在 wordpress 插件中包含 js 和 css [重复]
【发布时间】:2014-09-07 23:28:09
【问题描述】:

我是 Wordpress 的新手,我正在尝试将 js 和 css 文件以及 jQuery 包含到我创建的小部件中,我现在的代码是

<?php
class my_widget extends WP_Widget {

function __construct() {
    parent::__construct(
            // Base ID of your widget
            'my_widget', 

            // Widget name will appear in UI
            __('My Widget', 'my_widget_domain'), 

            // Widget description
            array( 'description' => __( 'Blah blah ', 'my_widget_domain' ), ) );
    }

        // Creating widget front-end
        // This is where the action happens
        public function widget( $args, $instance ) {

           //Some functionallity

        }

} // Class wpb_widget ends here

// Register and load the widget
function wpb_load_widget() {
    register_widget( 'my_widget' );
}
add_action( 'widgets_init', 'wpb_load_widget' );

?>
<link rel="stylesheet" href="<?php echo plugins_url();?>/my_plugin/js/jquery/jquery-ui.css">
<link rel="stylesheet" href="<?php echo plugins_url();?>/my_plugin/css/my_theme.css">
<script src="<?php echo plugins_url();?>/my_plugin/js/jquery/external/jquery/jquery.js"></script>
<script src="<?php echo plugins_url();?>/my_plugin/js/jquery/jquery-ui.js"></script>
<script src="<?php echo plugins_url();?>/my_plugin/js/myscript.js"></script>

如您所见,我在文件末尾包含了我的脚本,我读到了 wp_enqueue_script('jquery');,但我不确定在哪里包含我的脚本的正确位置。 wordpress 已经包含 jQueryUI 了吗?

【问题讨论】:

标签: jquery wordpress


【解决方案1】:

将 css 和 js 包含到 wordpress 插件中。

在插件文件夹的插件文件中添加以下代码。

add_action('init', 'register_script');
function register_script() {
    wp_register_script( 'custom_jquery', plugins_url('/js/script.js', __FILE__), array('jquery'), '2.5.1' );

    wp_register_style( 'new_style', plugins_url('/css/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' );
}

在插件目录的js和css目录下创建你的script.js和style.css文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-28
    • 2011-04-15
    • 2018-10-30
    • 2011-01-09
    • 1970-01-01
    • 2014-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多