文章目录
本文最后更新于 2022-11-16,已超过 365 天未更新,内容可能已失效。

wp_register_sidebar_widget( int|string $id, string $name, callable $output_callback, array $options = array(), mixed $params )

注册小部件的实例。
Register an instance of a widget.


说明

默认的widget选项是可以重写的“classname”。当$output_callback参数为空字符串时,该函数还可用于取消注册小部件。


源码(Source)

/**
 * Register an instance of a widget.
 *
 * The default widget option is 'classname' that can be overridden.
 *
 * The function can also be used to un-register widgets when `$output_callback`
 * parameter is an empty string.
 *
 * @since 2.2.0
 *
 * @global array $wp_registered_widgets       Uses stored registered widgets.
 * @global array $wp_register_widget_defaults Retrieves widget defaults.
 * @global array $wp_registered_widget_updates
 * @global array $_wp_deprecated_widgets_callbacks
 *
 * @param int|string $id              Widget ID.
 * @param string     $name            Widget display title.
 * @param callback   $output_callback Run when widget is called.
 * @param array      $options {
 *     Optional. An array of supplementary widget options for the instance.
 *
 *     @type string $classname   Class name for the widget's HTML container. Default is a shortened
 *                               version of the output callback name.
 *     @type string $description Widget description for display in the widget administration
 *                               panel and/or theme.
 * }
 */
function wp_register_sidebar_widget( $id, $name, $output_callback, $options = array() ) {
    global $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates, $_wp_deprecated_widgets_callbacks;
 
    $id = strtolower($id);
 
    if ( empty($output_callback) ) {
        unset($wp_registered_widgets[$id]);
        return;
    }
 
    $id_base = _get_widget_id_base($id);
    if ( in_array($output_callback, $_wp_deprecated_widgets_callbacks, true) && !is_callable($output_callback) ) {
        unset( $wp_registered_widget_controls[ $id ] );
        unset( $wp_registered_widget_updates[ $id_base ] );
        return;
    }
 
    $defaults = array('classname' => $output_callback);
    $options = wp_parse_args($options, $defaults);
    $widget = array(
        'name' => $name,
        'id' => $id,
        'callback' => $output_callback,
        'params' => array_slice(func_get_args(), 4)
    );
    $widget = array_merge($widget, $options);
 
    if ( is_callable($output_callback) && ( !isset($wp_registered_widgets[$id]) || did_action( 'widgets_init' ) ) ) {
 
        /**
         * Fires once for each registered widget.
         *
         * @since 3.0.0
         *
         * @param array $widget An array of default widget arguments.
         */
        do_action( 'wp_register_sidebar_widget', $widget );
        $wp_registered_widgets[$id] = $widget;
    }
}
更新版本源码位置使用被使用
5.3.0wp-includes/widgets.php613
CC 许可协议
署名-非商业性使用-相同方式共享 4.0 国际 了解协议
文章链接:https://zptheme.com/5473.html
本文作者:子佩·来源:子佩工作室

除非注明,子佩工作室 文章均为原创,转载请以链接形式标明本文地址。

相关推荐

登录后即可发表评论

社交账号登录

全部评论 (0)