# fetch_feed()

URL: https://chugunov.pro/api-wordpress/functions/fetch_feed/
Проверено на WordPress 6.9, обновлено 06.08.2026.
Источник: независимый русскоязычный справочник chugunov.pro. Не является официальной документацией WordPress.

Тип: функция.
Появился в версии: 2.8.0.

## Сигнатура

```php
fetch_feed( string|string[] $url ): SimplePieSimplePie|WP_Error
```

## Описание

Создаёт объект SimplePie на основе RSS- или Atom-фида по URL.

## Параметры

- `$url` `string|string[]` — обязательный. URL фида для получения. Если передан массив URL, фиды объединяются с помощью функции multifeed из SimplePie.
  
  См. также http://simplepie.org/wiki/faq/typical_multifeed_gotchas

## Возвращаемое значение

`SimplePieSimplePie|WP_Error` — WP_Error

## Исходный код

Файл: `wp-includes/feed.php:804`

```php
function fetch_feed( $url ) {
	if ( ! class_exists( 'SimplePie\SimplePie', false ) ) {
		require_once ABSPATH . WPINC . '/class-simplepie.php';
	}

	require_once ABSPATH . WPINC . '/class-wp-feed-cache-transient.php';
	require_once ABSPATH . WPINC . '/class-wp-simplepie-file.php';
	require_once ABSPATH . WPINC . '/class-wp-simplepie-sanitize-kses.php';

	$feed = new SimplePie\SimplePie();

	$feed->get_registry()->register( SimplePie\Sanitize::class, 'WP_SimplePie_Sanitize_KSES', true );

	/*
	 * We must manually overwrite $feed->sanitize because SimplePie's constructor
	 * sets it before we have a chance to set the sanitization class.
	 */
	$feed->sanitize = new WP_SimplePie_Sanitize_KSES();

	// Register the cache handler using the recommended method for SimplePie 1.3 or later.
	if ( method_exists( 'SimplePie_Cache', 'register' ) ) {
		SimplePie_Cache::register( 'wp_transient', 'WP_Feed_Cache_Transient' );
		$feed->set_cache_location( 'wp_transient' );
	} else {
		// Back-compat for SimplePie 1.2.x.
		require_once ABSPATH . WPINC . '/class-wp-feed-cache.php';
		$feed->set_cache_class( 'WP_Feed_Cache' );
	}

	$feed->get_registry()->register( SimplePie\File::class, 'WP_SimplePie_File', true );

	/** This filter is documented in wp-includes/class-wp-feed-cache-transient.php */
	$feed->set_cache_duration( apply_filters( 'wp_feed_cache_transient_lifetime', 12 * HOUR_IN_SECONDS, $url ) );

	/**
	 * Fires just before processing the SimplePie feed object.
	 *
	 * @since 3.0.0
	 *
	 * @param SimplePie\SimplePie $feed SimplePie feed object (passed by reference).
	 * @param string|string[]     $url  URL of feed or array of URLs of feeds to retrieve.
	 */
	do_action_ref_array( 'wp_feed_options', array( &$feed, $url ) );

	if ( empty( $url ) ) {
		/*
		 * @todo: Set $url to empty string once supported by SimplePie.
		 *
		 * The early return without proceeding is to work around a PHP 8.5
		 * deprecation issue resolved in https://github.com/simplepie/simplepie/pull/949
		 *
		 * To avoid the duplicate code, this block can be replaced with `$url = '';` once SimplePie
		 * is upgraded to a version that includes the fix.
		 */
		$feed->init();
		$feed->set_output_encoding( get_bloginfo( 'charset' ) );

		if ( $feed->error() ) {
			return new WP_Error( 'simplepie-error', $feed->error() );
		}

		return $feed;
	} elseif ( is_array( $url ) && count( $url ) === 1 ) {
		$url = array_shift( $url );
	} elseif ( is_array( $url ) ) {
		$feeds            = array();
		$simplepie_errors = array();
		foreach ( $url as $feed_url ) {
			$simplepie_instance = clone $feed;
			$simplepie_instance->set_feed_url( $feed_url );
			$simplepie_instance->init();
			$simplepie_instance->set_output_encoding( get_bloginfo( 'charset' ) );

			if ( $simplepie_instance->error() ) {
				$simplepie_errors[] = sprintf(
					/* translators: %1$s is the feed URL, %2$s is the error message. */
					__( 'Error fetching feed %1$s: %2$s' ),
					esc_url( $feed_url ),
					$simplepie_instance->error()
				);
				unset( $simplepie_instance );
				continue;
			}

			$feeds[] = $simplepie_instance;
			unset( $simplepie_instance );
		}

		if ( ! empty( $simplepie_errors ) ) {
			return new WP_Error( 'simplepie-error', $simplepie_errors );
		}

		$feed->init();
		$feed->data['items'] = SimplePie\SimplePie::merge_items( $feeds );
		return $feed;
	}

	$feed->set_feed_url( $url );
	$feed->init();
	$feed->set_output_encoding( get_bloginfo( 'charset' ) );

	if ( $feed->error() ) {
		return new WP_Error( 'simplepie-error', $feed->error() );
	}

	return $feed;
}
```

## История изменений

- 2.8.0 — Introduced.

## Связанные

Использует: [`do_action_ref_array`](https://chugunov.pro/api-wordpress/functions/do_action_ref_array/), [`__`](https://chugunov.pro/api-wordpress/functions/__/), [`esc_url`](https://chugunov.pro/api-wordpress/functions/esc_url/), [`get_bloginfo`](https://chugunov.pro/api-wordpress/functions/get_bloginfo/), [`apply_filters`](https://chugunov.pro/api-wordpress/functions/apply_filters/), `WP_Error::__construct`.
Используется в: [`wp_dashboard_rss_control`](https://chugunov.pro/api-wordpress/functions/wp_dashboard_rss_control/), [`wp_widget_rss_form`](https://chugunov.pro/api-wordpress/functions/wp_widget_rss_form/), [`wp_dashboard_plugins_output`](https://chugunov.pro/api-wordpress/functions/wp_dashboard_plugins_output/), `WP_Widget_RSS::widget`, [`wp_widget_rss_output`](https://chugunov.pro/api-wordpress/functions/wp_widget_rss_output/), [`wp_widget_rss_process`](https://chugunov.pro/api-wordpress/functions/wp_widget_rss_process/).

Оригинал в официальной документации: https://developer.wordpress.org/reference/functions/fetch_feed/
