fetch_feed( string|string[] $url ): SimplePie\SimplePie|WP_Error
- Since
- 2.8.0
- Source
wp-includes/feed.php:804
Compatibility
- WordPress
- since 2.8.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), 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
- Heavy
- Scaling
- Scales with input
- Instructions
- 82–106
- Plugin surface
- 2 hooks
- Called by
- 6
Reads stored settings via get_option(), cached per request but not free on a cold cache.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 194.
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.
6 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - optionoption read or write
get_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 · 16 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.
| When | Instructions | Calls it makes |
|---|---|---|
!method_exists() && empty($url) && !->error() | 82–87 | ->get_registry(), ->register(), method_exists(), ->set_cache_class(), ->get_registry(), ->register(), apply_filters(), ->set_cache_duration(), ->init(), get_bloginfo(), ->set_output_encoding(), ->error() |
method_exists() && empty($url) && !->error() | 82–87 | ->get_registry(), ->register(), method_exists(), ::SimplePie_Cache(), ->set_cache_location(), ->get_registry(), ->register(), apply_filters(), ->set_cache_duration(), ->init(), get_bloginfo(), ->set_output_encoding(), ->error() |
!method_exists() && !empty($url) && is_array($url) && !empty($simplepie_errors) | 85–94 | ->get_registry(), ->register(), method_exists(), ->set_cache_class(), ->get_registry(), ->register(), apply_filters(), ->set_cache_duration() |
method_exists() && !empty($url) && is_array($url) && !empty($simplepie_errors) | 85–94 | ->get_registry(), ->register(), method_exists(), ::SimplePie_Cache(), ->set_cache_location(), ->get_registry(), ->register(), apply_filters(), ->set_cache_duration() |
!method_exists() && empty($url) && ->error() | 88–93 | ->get_registry(), ->register(), method_exists(), ->set_cache_class(), ->get_registry(), ->register(), apply_filters(), ->set_cache_duration(), ->init(), get_bloginfo(), ->set_output_encoding(), ->error(), ->error(), do_action_ref_array() |
method_exists() && empty($url) && ->error() | 88–93 | ->get_registry(), ->register(), method_exists(), ::SimplePie_Cache(), ->set_cache_location(), ->get_registry(), ->register(), apply_filters(), ->set_cache_duration(), ->init(), get_bloginfo(), ->set_output_encoding(), ->error(), ->error(), do_action_ref_array() |
!method_exists() && !empty($url) && is_array($url) && empty($simplepie_errors) | 89–98 | ->get_registry(), ->register(), method_exists(), ->set_cache_class(), ->get_registry(), ->register(), apply_filters(), ->set_cache_duration(), ->init(), ::SimplePie\\SimplePie() |
method_exists() && !empty($url) && is_array($url) && empty($simplepie_errors) | 89–98 | ->get_registry(), ->register(), method_exists(), ::SimplePie_Cache(), ->set_cache_location(), ->get_registry(), ->register(), apply_filters(), ->set_cache_duration(), ->init(), ::SimplePie\\SimplePie() |
!method_exists() && !empty($url) && !is_array($url) && !->error() | 89–97 | ->get_registry(), ->register(), method_exists(), ->set_cache_class(), ->get_registry(), ->register(), apply_filters(), ->set_cache_duration(), ->set_feed_url(), ->init(), get_bloginfo(), ->set_output_encoding(), ->error() |
method_exists() && !empty($url) && !is_array($url) && !->error() | 89–97 | ->get_registry(), ->register(), method_exists(), ::SimplePie_Cache(), ->set_cache_location(), ->get_registry(), ->register(), apply_filters(), ->set_cache_duration(), ->set_feed_url(), ->init(), get_bloginfo(), ->set_output_encoding(), ->error() |
!method_exists() && !empty($url) && !is_array($url) && ->error() | 95–103 | ->get_registry(), ->register(), method_exists(), ->set_cache_class(), ->get_registry(), ->register(), apply_filters(), ->set_cache_duration(), ->set_feed_url(), ->init(), get_bloginfo(), ->set_output_encoding(), ->error(), ->error() |
!method_exists() && !empty($url) && is_array($url) && !->error() | 95–100 | ->get_registry(), ->register(), method_exists(), ->set_cache_class(), ->get_registry(), ->register(), apply_filters(), ->set_cache_duration(), array_shift(), ->set_feed_url(), ->init(), get_bloginfo(), ->set_output_encoding(), ->error() |
4 further outcomes, up to 106 instructions
method_exists() && !empty($url) && !is_array($url) && ->error() | 95–103 | ->get_registry(), ->register(), method_exists(), ::SimplePie_Cache(), ->set_cache_location(), ->get_registry(), ->register(), apply_filters(), ->set_cache_duration(), ->set_feed_url(), ->init(), get_bloginfo(), ->set_output_encoding(), ->error(), ->error() |
method_exists() && !empty($url) && is_array($url) && !->error() | 95–100 | ->get_registry(), ->register(), method_exists(), ::SimplePie_Cache(), ->set_cache_location(), ->get_registry(), ->register(), apply_filters(), ->set_cache_duration(), array_shift(), ->set_feed_url(), ->init(), get_bloginfo(), ->set_output_encoding(), ->error() |
!method_exists() && !empty($url) && is_array($url) && ->error() | 101–106 | ->get_registry(), ->register(), method_exists(), ->set_cache_class(), ->get_registry(), ->register(), apply_filters(), ->set_cache_duration(), array_shift(), ->set_feed_url(), ->init(), get_bloginfo(), ->set_output_encoding(), ->error(), ->error() |
method_exists() && !empty($url) && is_array($url) && ->error() | 101–106 | ->get_registry(), ->register(), method_exists(), ::SimplePie_Cache(), ->set_cache_location(), ->get_registry(), ->register(), apply_filters(), ->set_cache_duration(), array_shift(), ->set_feed_url(), ->init(), get_bloginfo(), ->set_output_encoding(), ->error(), ->error() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 194 | 82–106 | 12 | |
| 8.5 | 194 | 82–106 | 12 | |
| 8.4 | 194 | 82–106 | 12 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 197 | 85–109 | 12 | |
| 8.2 | 197 | 85–109 | 12 | |
| 8.1 | 197 | 85–109 | 12 | |
| 7.4 | 197 | 85–109 | 12 |
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:
- apply_filters( wp_feed_cache_transient_lifetime )filterline 836 (+32 into the body)
Filters the transient lifetime of the feed cache.
- do_action( wp_feed_options )actionline 846 (+42 into the body)
Fires just before processing the SimplePie feed object.
Uses · 10
- 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.
- __()Retrieves the translation of $text.
- esc_url()Checks and cleans a URL.
- \SimplePie\SimplePie::__construct()
- WP_SimplePie_Sanitize_KSES::__construct()
- SimplePie_Cache::register()
- WP_Error::__construct()Initializes the error.
- \SimplePie\SimplePie::merge_items()
Used by · 6
- WP_Widget_RSS::widget()Outputs the content for the current RSS widget instance.
- render_block_core_rss()Renders the `core/rss` block on server.
- wp_dashboard_plugins_output()Display plugins text for the WordPress news widget.
- wp_dashboard_rss_control()Sets up the RSS dashboard widget control and $args to be used as input to wp_widget_rss_form().
- wp_widget_rss_output()Displays the RSS entries in a list.
- wp_widget_rss_process()Processes RSS feed widget data and optionally retrieve feed items.
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->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() );Changelog
Introduced in 2.8.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 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.