Reputation: 307
Both websites are made with WordPress
I want to display a feed on my website on multiple locations with varying amount of items. The code I have below(functions.php) seems to be working, but I have a couple of questions about it. I am worried that the code is not optimal and may cause performance issues for the website. The website I use it for has about 100 thousands views a day.
The feed needs to be called on multiple locations sometimes on the same page. And sometimes it needs to show 3 items or 4 items.
To call the feed I use a WordPress function fetch_feed(); which works with SimplePie.
Where is the feed stored?
Is the feed being cashed or is there a request being done every time the website is loaded?
I would like the feed to have minimal impact on the performance of the website, how can I improve my code to achieve this?
My hosting provider uses server-side caching, is that going to be a problem for the feed?
Calling the feed
<?php Roots\Sage\Feed\update_feed(3); ?>
Code in functions.php:
function update_feed( $itemAmount ) {
include_once( ABSPATH . WPINC . '/feed.php' );
// Get a SimplePie feed object from the specified feed source.
$rss = fetch_feed( 'https://example.nl/feed/' );
$maxitems = 0;
$rss_items = "";
if ( ! is_wp_error( $rss ) ) : // Checks that the object is created correctly
// Figure out how many total items there are, but limit it to 5.
$maxitems = $rss->get_item_quantity( $itemAmount );
// Build an array of all the items, starting with element 0 (first element).
$rss_items = $rss->get_items( 0, $maxitems );
endif; ?>
<ul>
<?php if ( $maxitems == 0 ) : ?>
<li><?php _e( 'No items', 'sage' ); ?></li>
<?php else : ?>
<?php // Loop through each feed item and display each item as a hyperlink. ?>
<?php foreach ( $rss_items as $item ) :
$job_url = esc_url( $item->get_permalink() );
$job_title = esc_html( $item->get_title() );
$job_thumbnail_url = $item->get_item_tags( '', 'job_logo_url' );
?>
<li>
<a href="<?= esc_url( $job_url ); ?>" target="_blank"><img src="<?= $job_thumbnail_url[0]["data"]; ?>" alt=""></a>
</li>
<?php endforeach; ?>
<?php endif; ?>
</ul>
<?php
}
add_action( 'update_feed', __NAMESPACE__ . '\\update_feed' );
Upvotes: 2
Views: 149
Reputation: 8126
You are pretty much safe in terms of the number of requests, as fetch_feed()
automatically caches the fetched response.
Is the feed being cashed?
This is stated briefly in the Description of the fetch_feed()
function call, and also mentioned under the Notes section: fetch_feed
caches results for 12 hours by default. You can modify this by modifying the time interval via the filter wp_feed_cache_transient_lifetime
.
If you take a deeper look at the source code of the function, you will see that fetch_feed()
is really just a wrapper for the SimplePie class. Having said that, it is worth to read “How does SimplePie’s caching system work?” in their FAQ. Below is the essence.
- You tell SimplePie what feed you want to get and where to cache it.
- SimplePie looks to see if the feed is already cached:
- If the cache is fresh use that.
- If there is no cached copy at all, SimplePie will grab and cache the feed.
- If the cache is there but it's old (SimplePie defaults to 60 minutes; configurable with set_cache_duration()), then SimplePie will ask the feed if it has changed since the last time we grabbed it (this is the HTTPCG part).
- If it hasn't changed, we reset the timer on the cache's freshness and keep it for another 60 minutes before checking again.
- If the cache has changed, SimplePie dumps the existing cache (since the cache is just a copy of the data object based on the feed), and grabs a new copy of the feed and uses it.
Putting that together, it means that set_cache_duration()
of SimplePie is set to 12 hours by WordPress by default. Altogether this means that for 12 hours no new requests will be performed after the first request has been cached. This is not affected by how many feed items you request and display, as the entire feed is cached, not just the number of items you indicated.
Where is the feed stored?
By default the fetched feed content is stored in the database. To be precise, in the wp_options
table under a key that looks like _transient_feed_<hash>
(where <hash>
being a series of 32 characters).
How can I improve my code?
There is nothing much to improve. However, in case your feed does not change very often, you could increase the default 12 hours cache time. The example below increases that to 1 day, making use of the DAY_IN_SECONDS
constant.
add_filter('wp_feed_cache_transient_lifetime' , function ($seconds) {
return DAY_IN_SECONDS;
});
Is server side caching by the hosting provider going to be a problem for the feed?
No. Depending on the cache implementation it might have effect on how the transient actually will be stored (in memory, etc…), but it won’t affect the functionality.
Upvotes: 1