Warning: Trying to access array offset on false in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 216
register_widget() - 注册小工具 - WordPress函数 - WordPress动力
当前浏览:首页 / WordPress函数 / register_widget()

register_widget( string|WP_Widget $widget )

注册小工具


Warning: Undefined array key -1 in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/reference-cat.php on line 185

Warning: Undefined array key 2 in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/reference-cat.php on line 186

Warning: Undefined variable $searchand in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 154

Warning: Undefined array key "5eac3a4c3a88fe35eccfdb3bc6176d2d" in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 223

Warning: Undefined variable $searchand in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 154

Warning: Undefined array key "0ae79f97552e7204594f6cb278768be9" in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 223

registermore...

widget 小工具


描述

注册WP_Widget小工具

另见

  • WP_Widget

参数

$widget

(string|WP_Widget) (必填) WP_Widget子类的名称或实例。



源码

查看源码 官方文档


更新日志

版本描述
4.6.0更新了$widget参数,使其也接受WP_Widget实例对象,而不仅仅是WP_Widget子类名称
2.8.0开始引入

使用示例

  • 示例1

    示例

    <?php
    /**
     * Class WPDocs_New_Widget
     */
    class WPDocs_New_Widget extends WP_Widget {
    
    	/**
    	 * Constructs the new widget.
    	 *
    	 * @see WP_Widget::__construct()
    	 */
    	function __construct() {
    		// Instantiate the parent object.
    		parent::__construct( false, __( 'My New Widget Title', 'textdomain' ) );
    	}
    
    	/**
    	 * The widget's HTML output.
    	 *
    	 * @see WP_Widget::widget()
    	 *
    	 * @param array $args     Display arguments including before_title, after_title,
    	 *                        before_widget, and after_widget.
    	 * @param array $instance The settings for the particular instance of the widget.
    	 */
    	function widget( $args, $instance ) {}
    
    	/**
    	 * The widget update handler.
    	 *
    	 * @see WP_Widget::update()
    	 *
    	 * @param array $new_instance The new instance of the widget.
    	 * @param array $old_instance The old instance of the widget.
    	 * @return array The updated instance of the widget.
    	 */
    	function update( $new_instance, $old_instance ) {
    		return $new_instance;
    	}
    
    	/**
    	 * Output the admin widget options form HTML.
    	 *
    	 * @param array $instance The current widget settings.
    	 * @return string The HTML markup for the form.
    	 */
    	function form( $instance ) {
    		return '';
    	}
    }
    
    add_action( 'widgets_init', 'wpdocs_register_widgets' );
    
    /**
     * Register the new widget.
     *
     * @see 'widgets_init'
     */
    function wpdocs_register_widgets() {
    	register_widget( 'WPDocs_New_Widget' );
    }