wp_widget_rss_form()
概述
wp_widget_rss_form() 函数用于显示 RSS 小部件的选项表单,支持自定义字段显示和参数处理。
关键要点
- 函数显示 RSS 小部件的配置表单,包括 URL、标题、项目数等选项。
- 参数 $args 为必需,用于输入字段的值;$inputs 可选,用于覆盖默认显示选项。
- 默认显示选项包括 'url'、'title'、'items'、'show_summary'、'show_author'、'show_date',均为布尔值。
- 函数内部使用 wp_parse_args() 合并参数,并处理错误显示和表单输出。
代码示例
function wp_widget_rss_form( $args, $inputs = null ) {
$default_inputs = array(
'url' => true,
'title' => true,
'items' => true,
'show_summary' => true,
'show_author' => true,
'show_date' => true,
);
$inputs = wp_parse_args( $inputs, $default_inputs );
$args['title'] = isset( $args['title'] ) ? $args['title'] : '';
$args['url'] = isset( $args['url'] ) ? $args['url'] : '';
$args['items'] = isset( $args['items'] ) ? (int) $args['items'] : 0;
if ( $args['items'] < 1 || 20 < $args['items'] ) {
$args['items'] = 10;
}
// 更多表单输出代码...
}注意事项
- 函数自 WordPress 2.5.0 版本引入,用于 RSS 小部件表单生成。
- 相关函数包括 selected()、checked()、__() 等,用于 HTML 属性和文本处理。
- 被 wp_dashboard_rss_control() 和 WP_Widget_RSS::form() 调用,集成于仪表盘和小部件系统。
Displays RSS widget options form.
Description
The options for what fields are displayed for the RSS form are all booleans and are as follows: ‘url’, ‘title’, ‘items’, ‘show_summary’, ‘show_author’, ‘show_date’.
Parameters
$argsarray|stringrequired-
Values for input fields.
$inputsarrayoptional-
Override default display options.
Default:
null
Source
function wp_widget_rss_form( $args, $inputs = null ) {
$default_inputs = array(
'url' => true,
'title' => true,
'items' => true,
'show_summary' => true,
'show_author' => true,
'show_date' => true,
);
$inputs = wp_parse_args( $inputs, $default_inputs );$args['title'] = isset( $args['title'] ) ? $args['title'] : '';
$args['url'] = isset( $args['url'] ) ? $args['url'] : '';
$args['items'] = isset( $args['items'] ) ? (int) $args['items'] : 0;if ( $args['items'] < 1 || 20 < $args['items'] ) {
$args['items'] = 10;
}$args['show_summary'] = isset( $args['show_summary'] ) ? (int) $args['show_summary'] : (int) $inputs['show_summary'];
$args['show_author'] = isset( $args['show_author'] ) ? (int) $args['show_author'] : (int) $inputs['show_author'];
$args['show_date'] = isset( $args['show_date'] ) ? (int) $args['show_date'] : (int) $inputs['show_date'];if ( ! empty( $args['error'] ) ) {
echo '<p class="widget-error"><strong>' . __( 'RSS Error:' ) . '</strong> ' . esc_html( $args['error'] ) . '</p>';
}$esc_number = esc_attr( $args['number'] );
if ( $inputs['url'] ) :
?>
<p><label for="rss-url-<?php echo $esc_number; ?>"></label>
<input class="widefat" id="rss-url-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][url]" type="text" value="<?php echo esc_url( $args['url'] ); ?>" /></p><p><label for="rss-title-<?php echo $esc_number; ?>"></label>
<input class="widefat" id="rss-title-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][title]" type="text" value="<?php echo esc_attr( $args['title'] ); ?>" /></p><p><label for="rss-items-<?php echo $esc_number; ?>"></label>
<select id="rss-items-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][items]">
$i</option>";
}
?>
</select></p><p>
<input id="rss-show-summary-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][show_summary]" type="checkbox" value="1" <?php checked( $args['show_summary'] ); ?> />
<label for="rss-show-summary-<?php echo $esc_number; ?>"></label><br /><input id="rss-show-author-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][show_author]" type="checkbox" value="1" <?php checked( $args['show_author'] ); ?> />
<label for="rss-show-author-<?php echo $esc_number; ?>"></label><br /><input id="rss-show-date-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][show_date]" type="checkbox" value="1" <?php checked( $args['show_date'] ); ?> />
<label for="rss-show-date-<?php echo $esc_number; ?>"></label><br /></p>
<input type="hidden" id="rss-<?php echo esc_attr( $id ); ?>-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][<?php echo esc_attr( $input ); ?>]" value="<?php echo esc_attr( $args[ $input ] ); ?>" />
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-includes/widgets.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/widgets.php#L1710">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/widgets.php#L1710-L1777">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/functions/selected/">selected()</a><code>wp-includes/general-template.php
Outputs the HTML selected attribute.
checked() wp-includes/general-template.phpOutputs the HTML checked attribute.
__() wp-includes/l10n.phpRetrieves the translation of $text.
_e() wp-includes/l10n.phpDisplays translated text.
esc_html() wp-includes/formatting.phpEscaping for HTML blocks.
esc_attr() wp-includes/formatting.phpEscaping for HTML attributes.
esc_url() wp-includes/formatting.phpChecks and cleans a URL.
wp_parse_args() wp-includes/functions.phpMerges user defined arguments into defaults array.
| Used by | Description |
|---|---|
wp_dashboard_rss_control()wp-admin/includes/dashboard.php |
Sets up the RSS dashboard widget control and $args to be used as input to wp_widget_rss_form() . |
WP_Widget_RSS::form()wp-includes/widgets/class-wp-widget-rss.php |
Outputs the settings form for the RSS widget. |
Changelog
| Version | Description |
|---|---|
| 2.5.0 | Introduced. |