register_widget() - WordPress 函数参考中文文档

返回介绍

register_widget()

发布于 2017-09-11 字数2898 浏览 944 评论 0

register_widget( string $widget_class )

Register a widget


描述

Registers a WP_Widget widget


参数

$widget_class

(string)
(Required)
The name of a class that extends WP_Widget


源代码

File: wp-includes/widgets.php

function register_widget($widget_class) {
	global $wp_widget_factory;

	$wp_widget_factory->register($widget_class);
}

更新日志

Version 描述
2.8.0 Introduced.

相关函数

Uses

  • wp-includes/class-wp-widget-factory.php:
    WP_Widget_Factory::register()

Used By

  • wp-includes/widgets.php:
    wp_widgets_init()

User Contributed Notes

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note

    Contributed by Codex

    Example

    
    <?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' );
    }
    

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!