如何在我的插件中使用jQuery UI

时间:2013-04-28 作者:clockwiseq

当我在谷歌上搜索一些东西时,它什么也没有返回,这是世界上令人难过的一天。我正在尝试使用默认的日期选择器(或此时的任何日期选择器),但由于我缺乏Wordpress/PHP方面的知识,因此无法使用。在我的插件中,我试图注册jquery和ui,显然在此过程中破坏了WordPress的其他部分。有人能告诉我我做错了什么吗?如果他们能提供有效的解决方案,我将放弃所有代码:

add_action(\'init\', \'add_styles\');

function add_styles(){
    wp_deregister_style(\'simpleschoolstyles\');
    wp_register_style(\'simpleschoolstyles\', SSM_STYLEFILE);

    wp_deregister_script( \'jquery\' );
    wp_register_script( \'jquery\', \'http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js\');

    wp_deregister_script( \'jquery-ui\' );
    wp_register_script(\'jquery-ui\', \'http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js\');

    wp_deregister_style( \'jquery-ui\' );
    wp_register_style( \'jquery-ui\', \'http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/themes/pepper-grinder/jquery-ui.min.css\' );

    wp_deregister_script(\'maskedinput\');
    wp_register_script(\'maskedinput\', SSM_PLUGIN_URL . \'/includes/js/jquery.maskedinput.min.js\');

    wp_deregister_script(\'simpleschool\');
    wp_register_script(\'simpleschool\', SSM_PLUGIN_URL . \'/includes/js/simpleschool.js\');
}

add_action(\'wp_enqueue_scripts\', \'load_scripts\');
add_action(\'admin_enqueue_scripts\', \'load_scripts\');

function load_scripts()
{
    wp_enqueue_style(\'jquery-ui\');    
    wp_enqueue_style(\'simpleschoolstyles\');
    wp_enqueue_script(\'jquery\');       
    wp_enqueue_script(\'jquery-ui\');
    wp_enqueue_script(\'maskedinput\');
    wp_enqueue_script(\'simpleschool\');
}
我需要jQuery可以在管理区域以及前端用于用户数据输入。请有人帮忙。我几乎要把脚趾甲扯下来了,因为我已经把头发都扯下来了。。。我假设我一定是在错误的时间点排队,但由于我对WordPress的知识有限,我很快就给自己挖了个坟墓。

UPDATE:我修改了脚本,现在只加载jQuery UI,并测试了jQuery和UI都已加载,并且对这两个都成功,但DOM中没有现有对象:

add_action(\'init\', \'init_scripts\');

function init_scripts(){
    wp_deregister_style(\'simpleschoolstyles\');
    wp_register_style(\'simpleschoolstyles\', SSM_STYLEFILE);

    //wp_deregister_script( \'jquery-ui\' );
    wp_register_script(\'jquery-ui\', \'http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js\');

    //wp_deregister_style( \'jquery-ui\' );
    wp_register_style( \'jquery-ui-pepper-grinder\', \'http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/themes/pepper-grinder/jquery-ui.min.css\' );

    //wp_deregister_script(\'maskedinput\');
    wp_register_script(\'maskedinput\', SSM_PLUGIN_URL . \'/includes/js/jquery.maskedinput.min.js\');

    //wp_deregister_script(\'simpleschool\');
    wp_register_script(\'simpleschool\', SSM_PLUGIN_URL . \'/includes/js/simpleschool.js\');

    wp_enqueue_style(\'jquery-ui-pepper-grinder\');
    wp_enqueue_style(\'simpleschoolstles\');
    wp_enqueue_script( \'jquery\' );
    wp_enqueue_script( \'jquery-ui\' );
    wp_enqueue_script(\'simpleschool\');
}
我的测试:

jQuery(document).ready(function(){
    //jQuery(\'.datepick\').mask("99/99/9999");
    //jQuery(\'.phone\').mask("(999) 999-9999");
    jQuery( \'.datepick\' ).datepicker( \'option\', \'dateFormat\', \'yyyy-mm-dd\' ); // <-- this fails ????    
    alert(\'jQuery BETTER BE LOADED!!!\'); // <---this worked
    jQuery(\'<div>crazy wordpress and their php</div>\').dialog(); // <--- this worked too
});

3 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

考虑到datepicker需要的所有库are bundled with WordPress 并且是registered 有了所有适当的依赖关系,您真正需要做的就是:

function enqueue_my_scripts_wpse_97533() {
  wp_enqueue_script(\'jquery-ui-datepicker\');
}
add_action(\'wp_enqueue_scripts\',\'enqueue_my_scripts_wpse_97533\');
如果然后查看页面的源代码,您将看到jQuery、jQuery UI和jQuery UI日期选择器都已加载。

当然,您需要像现在这样加载任何其他脚本,尽管您应该用它们的依赖项(第三个参数)注册它们。

wp_register_script( $handle, $src, $deps, $ver, $in_footer ); 
例如。。。

wp_register_script(
    \'maskedinput\',
    SSM_PLUGIN_URL.\'/includes/js/jquery.maskedinput.min.js\',
    array(\'jquery\')
);
那样的话,你就可以把。。。

function enqueue_my_scripts_wpse_97533_v2() {
  wp_enqueue_script(\'maskedinput\');
}
add_action(\'wp_enqueue_scripts\',\'enqueue_my_scripts_wpse_97533_v2\');
。。。并且知道依赖项——jQuery——也会被加载。

SO网友:fuxia

To complement @s_ha_dum’s excellent answer, here is an example showing how to use the built-in jQuery UI date picker on your plugin page.

The result will look like this:

enter image description here

The most important parts:

  • Use your option page slug to enqueue the scripts and stylesheets on your page only, not on all admin pages (background).
  • Make sure to set datepicker({ dateFormat: "yy-mm-dd" }), so you know what to expect in your callback handler.
  • There is no built-in style for the date picker in WordPress, so you have to enqueue a separate stylesheet. But there is also a nice demo plugin from @helenhousandi with CSS that fits nicely into the core styles.

I built a base class first to have something I can use in other answers too and to keep the actual code for the date picker script specific and simple.

Base class Wpse_Plugin_Options_Page

/**
 *
 * We do not use the so called Settings API here, because that is way too
 * complicated.
 * admin-post.php is used instead: simple, clean markup, works.
 */
class Wpse_Plugin_Options_Page
{
    protected static $instance = NULL;
    protected $slug      = \'\';
    protected $menu_slug = \'wpse_demo\';
    protected $option    = \'wpse_option\';
    protected $title     = \'WPSE Demo\';
    protected $styles    = array();
    protected $scripts   = array();

    public static function get_instance()
    {
        NULL === self::$instance and self::$instance = new self;
        return self::$instance;
    }

    public function wp_loaded()
    {
        add_action(
            "admin_post_update_$this->option",
            array ( $this, \'admin_post\' )
        );
        add_action(
            \'admin_menu\',
            array ( $this, \'admin_menu\' )
        );
    }

    public function admin_menu()
    {
        $slug = add_options_page(
            $this->title,                       // page title
            $this->title,                       // menu title
            \'manage_options\',                   // capability
            $this->menu_slug,                   // menu slug
            array ( $this, \'render_page_base\' ) // callback function
        );

        $this->slug = $slug;

        add_action( "admin_print_styles-$slug",  array ( $this, \'enqueue_style\' ) );
        add_action( "admin_print_scripts-$slug", array ( $this, \'enqueue_script\' ) );
        add_action( "page_content_$slug",        array ( $this, \'render_page_content\' ) );
    }

    public function render_page_base()
    {
        $this->print_message();

        printf(
            \'<div class="wrap"><h2>%1$s</h2><form method="post" action="%2$s">\',
            $GLOBALS[\'title\'],
            admin_url( \'admin-post.php\' )
        );

        printf(
            \'<input type="hidden" name="action" value="%s"/>\',
            "update_$this->option"
        );
        wp_nonce_field( "update_$this->option" );

        do_action( \'page_content_\' . $this->slug );

        print \'</form></div>\';
    }

    protected function print_message()
    {
        if ( ! isset ( $_GET[\'msg\'] ) )
            return;

        $text = $this->get_message_text( $_GET[\'msg\'] );

        if ( ! $text )
            return;

        print "<div id=\'message\' class=\'updated fade\'><p>$text</p></div>";
    }

    protected function get_message_text( $id )
    {
        $messages = $this->get_messages();

        if ( isset ( $messages[ $id ] ) )
            return $messages[ $id ];

        return FALSE;
    }

    protected function get_messages()
    {
        return array();
    }

    public function render_page_content()
    {
        echo $this->slug;
    }

    public function enqueue_style()
    {
        foreach ( $this->styles as $style )
            wp_enqueue_style( $style );

        do_action( \'base_styles_loaded_\' . $this->slug );
    }

    public function enqueue_script()
    {
        foreach ( $this->scripts as $script )
            wp_enqueue_script( $script );

        do_action( \'base_scripts_loaded_\' . $this->slug );
    }

    public function admin_post()
    {
        if ( ! check_admin_referer( "update_$this->option" ) )
            die( \'nope\' );

        if ( ! isset ( $_POST[ $this->option ] ) )
            die( \'something is missing\' );

        $msg = $this->save_option( $_POST[ $this->option ] );

        $url = add_query_arg( \'msg\', $msg, $_POST[ \'_wp_http_referer\' ] );

        wp_safe_redirect( $url, 303 );
        exit;
    }

    protected function save_option( $data )
    {
        return (bool) update_option( $this->option, $data );
    }
}

Now we have to redefine only the most important pieces. Nice and short.

Special class Wpse_Datepicker_Example

class Wpse_Datepicker_Example extends Wpse_Plugin_Options_Page
{
    protected $title     = \'jQuery Date Picker\';
    protected $menu_slug = \'wpse_datepicker\';
    protected $option    = \'wpse_datepicker\';
    protected $scripts   = array ( \'jquery-ui-datepicker\' );

    // not inherited
    public static function get_instance()
    {
        NULL === self::$instance and self::$instance = new self;
        return self::$instance;
    }

    public function render_page_content()
    {
        $value = esc_attr( get_option( $this->option ) );
        printf(
            \'<p style="margin:100px auto;width:30em"><label for="%1$s">Pick a date
                <input type="text" id="%1$s" name="%2$s" value="%3$s" />
            </label> %4$s</p>\',
            \'datepicker\',
            $this->option,
            $value,
            get_submit_button( \'Save\', \'primary\', \'submit\', FALSE )
        );

        add_action(
            "admin_footer-$this->slug",
            array ( $this, \'print_footer_script\' )
        );
    }

    public function enqueue_style()
    {
        wp_register_style(
            \'jquery-ui-datepicker\',
            \'http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/themes/pepper-grinder/jquery-ui.min.css\'
        );
        wp_enqueue_style( \'jquery-ui-datepicker\' );
    }

    public function print_footer_script()
    {
        ?>
<script>
jQuery( "#datepicker" ).datepicker({ dateFormat: "yy-mm-dd" });
</script>
        <?php
    }

    protected function get_messages()
    {
        return array (
            1 => \'Date saved.\'
        );
    }
}

There is still much room for improvements, but as a start it should be useful.

SO网友:csehasib

有几种方法可以将jQuery包含到主题中。我总是使用WP捆绑版,我觉得很简单。为了正确地进行设置,我们需要确保WP页面在页面加载中包含以下文件。用于加载防尘套脚本(&M);在主题函数上添加风箱代码。php文件。

Script for front-end use

function add_e2_date_picker(){
//jQuery UI date picker file
wp_enqueue_script(\'jquery-ui-datepicker\');
//jQuery UI theme css file
wp_enqueue_style(\'e2b-admin-ui-css\',\'http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.0/themes/base/jquery-ui.css\',false,"1.9.0",false);
}
add_action(\'wp_enqueue_scripts\', \'add_e2_date_picker\'); 

Script for back-end use

function add_e2_date_picker(){
//jQuery UI date picker file
wp_enqueue_script(\'jquery-ui-datepicker\');
//jQuery UI theme css file
wp_enqueue_style(\'e2b-admin-ui-css\',\'http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.0/themes/base/jquery-ui.css\',false,"1.9.0",false);
}
add_action(\'admin_enqueue_scripts\', \'add_e2_date_picker\'); 
我们可以为特定页面编写一个挂钩函数,例如single。php、页面或插件页面。我已经添加或挂接了“选项常规”。用于显示的phpSettigns->Date Picker. 只要把这段代码也放进去就行了。php文件或下面的代码。

function register_datepiker_submenu() {
    add_submenu_page( \'options-general.php\', \'Date Picker\', \'Date Picker\', \'manage_options\', \'date-picker\', \'datepiker_submenu_callback\' );
}

function datepiker_submenu_callback() { ?>

    <div class="wrap">

    <input type="text" class="datepicker" name="datepicker" value=""/>

    </div>

    <script>
    jQuery(function() {
        jQuery( ".datepicker" ).datepicker({
            dateFormat : "dd-mm-yy"
        });
    });
    </script> 

<?php }
add_action(\'admin_menu\', \'register_datepiker_submenu\');

?>
添加此代码后,您将在Admin Menu->Settigns->Date Picker. 如果您需要获得此选项的任何帮助,请询问任何查询并在Add a jQuery DatePicker to WordPress Theme or Plugin.

结束

相关推荐

加载最新版本的jQuery时出错

我正在尝试设置WordPress,以便它使用最新版本的jQuery。以下是我迄今为止所做的工作:功能。php<? add_action( \'wp_enqueue_scripts\', \'RegisterJQuery\' ); add_action( \'wp_enqueue_scripts\', \'EnqueueScripts\' ); function RegisterJQuery() { wp_deregister_script(