函数文档

wp_widget_rss_process()

💡 云策文档标注

概述

wp_widget_rss_process() 函数用于处理 RSS 小部件数据,并可选择性地检索 feed 项目。它验证输入参数,处理 feed 错误,并返回包含 feed 信息的数组。

关键要点

  • 函数处理 RSS 小部件数据,限制 feed 项目数不超过 20 个,否则重置为默认值 10。
  • 返回数组包含 feed 标题、URL、链接、项目、错误状态以及显示摘要、作者和日期的选项。
  • 参数包括必需的 $widget_rss 数组(未转义数据)和可选的 $check_feed 布尔值(默认 true 以检查 feed 错误)。
  • 使用 fetch_feed() 获取 feed,并处理可能的 WP_Error 对象。

代码示例

function wp_widget_rss_process( $widget_rss, $check_feed = true ) {
	$items = (int) $widget_rss['items'];
	if ( $items < 1 || $items > 20 ) {
		$items = 10;
	}
	$url          = esc_url_raw( $widget_rss['url'] );
	$title        = isset( $widget_rss['title'] ) ? trim( $widget_rss['title'] ) : '';
	$show_summary = isset( $widget_rss['show_summary'] ) ? (int) $widget_rss['show_summary'] : 0;
	$show_author  = isset( $widget_rss['show_author'] ) ? (int) $widget_rss['show_author'] : 0;
	$show_date    = isset( $widget_rss['show_date'] ) ? (int) $widget_rss['show_date'] : 0;
	$error        = false;
	$link         = '';

	if ( $check_feed ) {
		$rss = fetch_feed( $url );
		if ( is_wp_error( $rss ) ) {
			$error = $rss->get_error_message();
		} else {
			$link = esc_url( strip_tags( $rss->get_permalink() ) );
			while ( stristr( $link, 'http' ) !== $link ) {
				$link = substr( $link, 1 );
			}

			$rss->__destruct();
			unset( $rss );
		}
	}

	return compact( 'title', 'url', 'link', 'items', 'error', 'show_summary', 'show_author', 'show_date' );
}

注意事项

  • 确保 $widget_rss 数组包含有效的 'items' 和 'url' 键,否则可能导致意外行为。
  • 当 $check_feed 为 true 时,函数会调用 fetch_feed(),这可能涉及网络请求,影响性能。
  • 返回的数组元素顺序固定,便于开发者直接使用。

📄 原文内容

Processes RSS feed widget data and optionally retrieve feed items.

Description

The feed widget can not have more than 20 items or it will reset back to the default, which is 10.

The resulting array has the feed title, feed url, feed link (from channel), feed items, error (if any), and whether to show summary, author, and date.
All respectively in the order of the array elements.

Parameters

$widget_rssarrayrequired
RSS widget feed data. Expects unescaped data.
$check_feedbooloptional
Whether to check feed for errors.

Default:true

Return

array

Source

function wp_widget_rss_process( $widget_rss, $check_feed = true ) {
	$items = (int) $widget_rss['items'];
	if ( $items < 1 || 20 < $items ) {
		$items = 10;
	}
	$url          = sanitize_url( strip_tags( $widget_rss['url'] ) );
	$title        = isset( $widget_rss['title'] ) ? trim( strip_tags( $widget_rss['title'] ) ) : '';
	$show_summary = isset( $widget_rss['show_summary'] ) ? (int) $widget_rss['show_summary'] : 0;
	$show_author  = isset( $widget_rss['show_author'] ) ? (int) $widget_rss['show_author'] : 0;
	$show_date    = isset( $widget_rss['show_date'] ) ? (int) $widget_rss['show_date'] : 0;
	$error        = false;
	$link         = '';

	if ( $check_feed ) {
		$rss = fetch_feed( $url );

		if ( is_wp_error( $rss ) ) {
			$error = $rss->get_error_message();
		} else {
			$link = esc_url( strip_tags( $rss->get_permalink() ) );
			while ( stristr( $link, 'http' ) !== $link ) {
				$link = substr( $link, 1 );
			}

			$rss->__destruct();
			unset( $rss );
		}
	}

	return compact( 'title', 'url', 'link', 'items', 'error', 'show_summary', 'show_author', 'show_date' );
}

Changelog

Version Description
2.5.0 Introduced.