register_widget()

最后更新于:2021-11-27 21:39:20

register_widget( string|GC_Widget$widget)

Register a widget

参数

$widget

(string|GC_Widget) (Required) Either the name of a GC_Widget subclass or an instance of a GC_Widget subclass.

源文件

文件: gc-includes/widgets.php

function register_widget( $widget ) {
	global $gc_widget_factory;

	$gc_widget_factory->register( $widget );
}
<?php
/**
 * Class GCDocs_New_Widget
 */
class GCDocs_New_Widget extends GC_Widget {

	/**
	 * Constructs the new widget.
	 *
	 * @see GC_Widget::__construct()
	 */
	function __construct() {
		// Instantiate the parent object.
		parent::__construct( false, __( 'My New Widget Title', 'textdomain' ) );
	}

	/**
	 * The widget's HTML output.
	 *
	 * @see GC_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 GC_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', 'gcdocs_register_widgets' );

/**
 * Register the new widget.
 *
 * @see 'widgets_init'
 */
function gcdocs_register_widgets() {
	register_widget( 'GCDocs_New_Widget' );
}