单个插件中的WordPress多个窗口小部件

时间:2012-03-31 作者:Ajay Patel

在我的插件中需要创建两个小部件。一个是“社会等级”,另一个是“个人资料等级”。

这是我在这里使用的代码。

    <?php
    add_action( \'widgets_init\', \'src_load_widgets\' );

function src_load_widgets() {
    register_widget( \'Widget_SRC\' );
}
function src_load_widgets() {
    register_widget( \'Widget_Rank \' );
}

class Widget_SRC extends WP_Widget {

    function Widget_SRC() {

        $widget_ops = array( \'classname\' => \'src\', \'description\' => __(\'Several social networks counts and ranking system\', \'src\') );       
        $control_ops = array( \'width\' => 300, \'height\' => 350, \'id_base\' => \'social-rank\' );        
        $this->WP_Widget( \'social-rank\', __(\'Social Rank\', \'src\'), $widget_ops, $control_ops );
    }


    function widget( $args, $instance ) {
        extract( $args );   
        $title = apply_filters(\'widget_title\', $instance[\'title\'] );    
        echo $before_widget;

        if ( $title )
            echo $before_title . $title . $after_title;

        echo do_shortcode(\'[SocialRank]\');      

        echo $after_widget;
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;

        $instance[\'title\'] = strip_tags( $new_instance[\'title\'] );
        return $instance;
    }

    function form( $instance ) {

        $defaults = array( \'title\' => __(\'Social Rank\', \'src\'));
        $instance = wp_parse_args( (array) $instance, $defaults ); ?>
        <!-- Widget Title: Text Input -->
        <p>
            <label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php _e(\'Title:\', \'hybrid\'); ?></label>
            <input id="<?php echo $this->get_field_id( \'title\' ); ?>" name="<?php echo $this->get_field_name( \'title\' ); ?>" value="<?php echo $instance[\'title\']; ?>" style="width:100%;" />
        </p>        

    <?php
    }
}
        class Widget_Rank extends WP_Widget {

            /**
             * Widget setup.
             */
            function Widget_Rank() {
                /* Widget settings. */
                $widget_ops = array( \'classname\' => \'rank\', \'description\' => __(\'Display profile rank based on vote & social counts.\', \'rank\') );

                /* Widget control settings. */
                $control_ops = array( \'width\' => 300, \'height\' => 350, \'id_base\' => \'profile-rank\' );

                /* Create the widget. */
                $this->WP_Widget( \'profile-rank\', __(\'Profile Rank\', \'rank\'), $widget_ops, $control_ops );
            }

            /**
             * How to display the widget on the screen.
             */
            function widget( $args, $instance ) {
                extract( $args );

                /* Our variables from the widget settings. */
                $title = apply_filters(\'widget_title\', $instance[\'title\'] );

                /* Before widget (defined by themes). */
                echo $before_widget;

                /* Display the widget title if one was input (before and after defined by themes). */
                if ( $title )
                    echo $before_title . $title . $after_title;

                echo do_shortcode(\'[SocialRank]\');

                /* After widget (defined by themes). */
                echo $after_widget;
            }

            /**
             * Update the widget settings.
             */
            function update( $new_instance, $old_instance ) {
                $instance = $old_instance;

                /* Strip tags for title and name to remove HTML (important for text inputs). */
                $instance[\'title\'] = strip_tags( $new_instance[\'title\'] );
                return $instance;
            }

            /**
             * Displays the widget settings controls on the widget panel.
             * Make use of the get_field_id() and get_field_name() function
             * when creating your form elements. This handles the confusing stuff.
             */
            function form( $instance ) {

                /* Set up some default widget settings. */
                $defaults = array( \'title\' => __(\'Profile Rank\', \'rank\'));
                $instance = wp_parse_args( (array) $instance, $defaults ); ?>

                <!-- Widget Title: Text Input -->
                <p>
                    <label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php _e(\'Title:\', \'hybrid\'); ?></label>
                    <input id="<?php echo $this->get_field_id( \'title\' ); ?>" name="<?php echo $this->get_field_name( \'title\' ); ?>" value="<?php echo $instance[\'title\']; ?>" style="width:100%;" />
                </p>        

            <?php
            }
        }
         ?>
但它并没有向我显示第二个小部件。我发现了这个问题,但无法找到解决方法https://stackoverflow.com/questions/5247302/php-namespace-5-3-and-wordpress-widget/5247436#5247436

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

在一个文件中注册几个小部件绝对没有问题。小部件是独立的类,与其他编程语言不同,PHP在一个文件中声明两个类不会有问题。

add_action( \'widgets_init\', \'src_load_widgets\' );

function src_load_widgets() {
    register_widget( \'Widget_SRC\' );
    register_widget( \'Widget_Rank \' );
}

class Widget_SRC extends WP_Widget {
    // ...
}

class Widget_Rank extends WP_Widget {
    // ...
}

SO网友:Ajay Patel

Ohh my mistake

function load_widgets() {
    register_widget( \'Widget_SRC\' );
    register_widget( \'Widget_Rank\' );
}
结束

相关推荐

Using tabs in admin widgets

更新:我应该指出,我在插件/主题选项页面上测试了下面的方法,效果非常好。事实上,我还能够包括jQuery cookie插件,以保留页面加载之间的当前选项卡选择(很好!)。当您有多个小部件选项时,尝试向管理小部件添加选项卡以减少表单占用空间。我正在使用WordPress附带的jQuery UI选项卡。以下是我迄今为止在插件中编写的代码。http://pastebin.com/fe4wdBcp当小部件被拖动到小部件区域时,选项卡似乎呈现为OK,但您无法单击查看第二个选项卡。更糟糕的是,如果刷新窗口小部件页面,