wppaste
WordPress

fetch_feed( string|string[] $url ): SimplePie\SimplePie|WP_Error

Since
2.8.0
Source
wp-includes/feed.php:800
Builds SimplePie object based on RSS or Atom feed from URL.

Compatibility

WordPress
since 2.8.0
PHP
7.4–8.6-dev
  • 6.7.10
  • 6.8.11
  • 6.9.10
  • 7.0.7
  • 7.1.3

Present in every tracked release (6.7.10 to 7.1.3), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$urlstring|string[]
URL of feed to retrieve. If an array of URLs, the feeds are merged using SimplePie's multifeed feature.
See also http://simplepie.org/wiki/faq/typical_multifeed_gotchas

Return value

SimplePie\SimplePie|WP_Error
SimplePie object on success or WP_Error object on failure.

Performance profile

How much work a call to fetch_feed() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Moderate

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
75–86

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 95.

Plugin surface
2 hooks

Third-party callbacks on 'wp_feed_cache_transient_lifetime', 'wp_feed_options' run inside this call, and their cost is not bounded by anything here.

Called by
6

6 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • optionoption read or writeget_option()one call below fetch_feed()

Further down the call graph this can also reach cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 4 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost fetch_feed() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!method_exists() && !->error()75–80->set_sanitize_class(), method_exists(), ->set_cache_class(), ->set_file_class(), ->set_feed_url(), apply_filters(), ->set_cache_duration(), ->init(), get_bloginfo(), ->set_output_encoding(), ->error()
method_exists() && !->error()75–80->set_sanitize_class(), method_exists(), ::SimplePie_Cache(), ->set_cache_location(), ->set_file_class(), ->set_feed_url(), apply_filters(), ->set_cache_duration(), ->init(), get_bloginfo(), ->set_output_encoding(), ->error()
!method_exists() && ->error()81–86->set_sanitize_class(), method_exists(), ->set_cache_class(), ->set_file_class(), ->set_feed_url(), apply_filters(), ->set_cache_duration(), ->init(), get_bloginfo(), ->set_output_encoding(), ->error(), ->error(), do_action_ref_array()
method_exists() && ->error()81–86->set_sanitize_class(), method_exists(), ::SimplePie_Cache(), ->set_cache_location(), ->set_file_class(), ->set_feed_url(), apply_filters(), ->set_cache_duration(), ->init(), get_bloginfo(), ->set_output_encoding(), ->error(), ->error(), do_action_ref_array()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev9575–863
8.59575–863
8.49575–8633 fewer instructions than PHP 8.3
8.39878–893
8.29878–893
8.19878–893
7.49878–893

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 2

2 hooks fire while fetch_feed() runs, in this order:

  1. apply_filters( wp_feed_cache_transient_lifetime )filterline 832 (+32 into the body)

    Filters the transient lifetime of the feed cache.

  2. do_action( wp_feed_options )actionline 842 (+42 into the body)

    Fires just before processing the SimplePie feed object.

Uses · 7

  • apply_filters()Calls the callback functions that have been added to a filter hook.
  • do_action_ref_array()Calls the callback functions that have been added to an action hook, specifying arguments in an array.
  • get_bloginfo()Retrieves information about the current site.
  • \SimplePie\SimplePie::__construct()
  • WP_SimplePie_Sanitize_KSES::__construct()
  • SimplePie_Cache::register()
  • WP_Error::__construct()Initializes the error.

Used by · 6

Source code

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->set_sanitize_class( 'WP_SimplePie_Sanitize_KSES' );	/*	 * 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->set_file_class( 'WP_SimplePie_File' ); 	$feed->set_feed_url( $url );	/** 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 ) ); 	$feed->init();	$feed->set_output_encoding( get_bloginfo( 'charset' ) ); 	if ( $feed->error() ) {		return new WP_Error( 'simplepie-error', $feed->error() );	} 	return $feed;}

Changelog

Introduced in 2.8.0. Unchanged from 6.7.10 through 7.1.3.

  1. 6.7.10
  2. 6.8.11
  3. 6.9.10
  4. 7.0.7
  5. 7.1.3

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.7.10 tag, from src/wp-includes/feed.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.