WP_Widget_Meta
概述
WP_Widget_Meta 是 WordPress 核心类,用于实现 Meta 小工具,显示登录/登出、RSS 链接等内容。它继承自 WP_Widget,提供小工具的基本功能。
关键要点
- WP_Widget_Meta 是一个核心小工具类,用于显示 Meta 相关链接,如登录、RSS 和 WordPress.org 链接。
- 类继承自 WP_Widget,遵循 WordPress 小工具的标准结构,包括构造方法、widget、update 和 form 方法。
- widget 方法输出小工具内容,支持 HTML5 和 XHTML 格式,并应用过滤器如 widget_title 和 navigation_widgets_format。
- update 方法处理设置更新,对标题进行 sanitize_text_field 清理。
- form 方法输出设置表单,使用 wp_parse_args 处理默认值。
代码示例
class WP_Widget_Meta extends WP_Widget {
public function __construct() {
$widget_ops = array(
'classname' => 'widget_meta',
'description' => __( 'Login, RSS, & WordPress.org links.' ),
'customize_selective_refresh' => true,
'show_instance_in_rest' => true,
);
parent::__construct( 'meta', __( 'Meta' ), $widget_ops );
}
public function widget( $args, $instance ) {
$default_title = __( 'Meta' );
$title = ! empty( $instance['title'] ) ? $instance['title'] : $default_title;
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
echo $args['before_widget'];
if ( $title ) {
echo $args['before_title'] . $title . $args['after_title'];
}
$format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
$format = apply_filters( 'navigation_widgets_format', $format );
if ( 'html5' === $format ) {
$title = trim( strip_tags( $title ) );
$aria_label = $title ? $title : $default_title;
echo '';
}
?>
<ul>
<?php wp_register(); ?>
<li><?php wp_loginout(); ?></li>
<li><a href="<?php echo esc_url( get_bloginfo( 'rss2_url' ) ); ?>"><?php _e( 'Entries feed' ); ?></a></li>
<li><a href="<?php echo esc_url( get_bloginfo( 'comments_rss2_url' ) ); ?>"><?php _e( 'Comments feed' ); ?></a></li>
<?php
echo apply_filters(
'widget_meta_poweredby',
sprintf(
'<li><a href="%1$s">%2$s</a></li>',
esc_url( __( 'https://wordpress.org/' ) ),
__( 'WordPress.org' )
),
$instance
);
wp_meta();
?>
</ul>
<?php
if ( 'html5' === $format ) {
echo '';
}
echo $args['after_widget'];
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = sanitize_text_field( $new_instance['title'] );
return $instance;
}
public function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" />
</p>
<?php
}
}注意事项
- WP_Widget_Meta 自 WordPress 2.8.0 引入,是核心小工具的一部分,不应直接修改,可通过子类或过滤器自定义。
- widget 方法中使用了多个过滤器,如 widget_title 和 navigation_widgets_format,开发者可以利用这些过滤器调整输出。
- update 方法仅处理标题字段,确保数据安全,其他设置需根据需求扩展。
- form 方法使用 wp_parse_args 确保实例设置具有默认值,避免未定义错误。
Core class used to implement a Meta widget.
Description
Displays log in/out, RSS feed links, etc.
See also
Methods
| Name | Description |
|---|---|
| WP_Widget_Meta::__construct | Sets up a new Meta widget instance. |
| WP_Widget_Meta::form | Outputs the settings form for the Meta widget. |
| WP_Widget_Meta::update | Handles updating settings for the current Meta widget instance. |
| WP_Widget_Meta::widget | Outputs the content for the current Meta widget instance. |
Source
class WP_Widget_Meta extends WP_Widget {
/**
* Sets up a new Meta widget instance.
*
* @since 2.8.0
*/
public function __construct() {
$widget_ops = array(
'classname' => 'widget_meta',
'description' => __( 'Login, RSS, & WordPress.org links.' ),
'customize_selective_refresh' => true,
'show_instance_in_rest' => true,
);
parent::__construct( 'meta', __( 'Meta' ), $widget_ops );
}
/**
* Outputs the content for the current Meta widget instance.
*
* @since 2.8.0
*
* @param array $args Display arguments including 'before_title', 'after_title',
* 'before_widget', and 'after_widget'.
* @param array $instance Settings for the current Meta widget instance.
*/
public function widget( $args, $instance ) {
$default_title = __( 'Meta' );
$title = ! empty( $instance['title'] ) ? $instance['title'] : $default_title;
/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
echo $args['before_widget'];
if ( $title ) {
echo $args['before_title'] . $title . $args['after_title'];
}
$format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
/** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */
$format = apply_filters( 'navigation_widgets_format', $format );
if ( 'html5' === $format ) {
// The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
$title = trim( strip_tags( $title ) );
$aria_label = $title ? $title : $default_title;
echo '<nav aria-label="' . esc_attr( $aria_label ) . '">';
}
?>
<ul>
<li></li>
<li><a href="<?php echo esc_url( get_bloginfo( 'rss2_url' ) ); ?>"></a></li>
<li><a href="<?php echo esc_url( get_bloginfo( 'comments_rss2_url' ) ); ?>"></a></li>
<a href="%1$s">%2$s</a></li>',
esc_url( __( 'https://wordpress.org/' ) ),
__( 'WordPress.org' )
),
$instance
);
wp_meta();
?>
</ul>
';
}
echo $args['after_widget'];
}
/**
* Handles updating settings for the current Meta widget instance.
*
* @since 2.8.0
*
* @param array $new_instance New settings for this instance as input by the user via
* WP_Widget::form().
* @param array $old_instance Old settings for this instance.
* @return array Updated settings to save.
*/
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = sanitize_text_field( $new_instance['title'] );
return $instance;
}
/**
* Outputs the settings form for the Meta widget.
*
* @since 2.8.0
*
* @param array $instance Current settings.
*/
public function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" />
</p>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-includes/widgets/class-wp-widget-meta.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/widgets/class-wp-widget-meta.php#L19">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/widgets/class-wp-widget-meta.php#L19-L143">View on GitHub</a></p></section>
<section class="wp-block-wporg-code-reference-related" data-nosnippet="true"><h2 id="related" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#related">Related</a></h2> <section style="margin-top:var(--wp--preset--spacing--20)" class="wp-block-wporg-code-table" id="uses"><figure class="wp-block-table "><table><thead><tr><th scope="col">Uses</th><th scope="col">Description</th></tr></thead><tbody><tr class=""><td><a href="https://developer.wordpress.org/reference/classes/wp_widget/">WP_Widget</a><code>wp-includes/class-wp-widget.php
Core base class extended to register widgets.
Changelog
| Version | Description |
|---|---|
| 2.8.0 | Introduced. |