From a CORS Proxy to Cached Transients
An earlier CSS-Tricks experiment showed how to surface liked items from an RSS reader as a public "Likes" feed. In that demo, the author flagged two problems: it relied on a public CORS proxy, unsuitable for production on security grounds, and suggested fetching and caching via WordPress transients as a workaround.
This implementation follows that second path. The data is served through a WordPress block, displayed here inside a <details> element, and capped at 10 items per fetch rather than an unbounded list, since pulling everything carries a performance cost.
The original demo did most of the work. The harder parts were fitting it into a WordPress block architecture and getting transients running for the first time.
Fetching and Caching
The function that wraps the caching logic is shown below. It is not necessarily the most efficient version, but it reflects the author's own reading of how the pieces fit together.
function fetch_and_store_data() {
$transient_key = 'fetched_data';
$cached_data = get_transient($transient_key);
if ($cached_data) {
return new WP_REST_Response($cached_data, 200);
}
$response = wp_remote_get('https://feedbin.com/starred/a22c4101980b055d688e90512b083e8d.xml');
if (is_wp_error($response)) {
return new WP_REST_Response('Error fetching data', 500);
}
$body = wp_remote_retrieve_body($response);
$data = simplexml_load_string($body, 'SimpleXMLElement', LIBXML_NOCDATA);
$json_data = json_encode($data);
$array_data = json_decode($json_data, true);
$items = [];
foreach ($array_data['channel']['item'] as $item) {
$items[] = [
'title' => $item['title'],
'link' => $item['link'],
'pubDate' => $item['pubDate'],
'description' => $item['description'],
];
}
set_transient($transient_key, $items, 12 * HOUR_IN_SECONDS);
return new WP_REST_Response($items, 200);
}
add_action('rest_api_init', function () {
register_rest_route('custom/v1', '/fetch-data', [
'methods' => 'GET',
'callback' => 'fetch_and_store_data',
]);
});
function fetch_and_store_data() {
}
The setup begins with the function name and two variables:
$transient_key = 'fetched_data';
$cached_data = get_transient($transient_key);
$transient_key is the identifier used when setting and getting the transient, and $cached_data already acts as the getter, so that half of the job is done. A conditional checks that cached data exists before using it:
if ($cached_data) {
return new WP_REST_Response($cached_data, 200);
}
This also establishes a new WordPress REST API response, which is where the data gets cached. Instead of reading directly from Feedbin, the response is formed from Feedbin as the origin; with the starred items stored locally on the same domain, CORS stops being an issue. wp_remote_get() builds that response:
$response = wp_remote_get('https://feedbin.com/starred/a22c4101980b055d688e90512b083e8d.xml');
An error is thrown if there is no $response, meaning no fresh $cached_data is available — something worth knowing immediately:
if (is_wp_error($response)) {
return new WP_REST_Response('Error fetching data', 500);
}
Parsing the Feed
Most of the remaining work is turning the XML returned by Feedbin into JSON, looping over each item to extract its title, link, publish date, and description:
$body = wp_remote_retrieve_body($response);
$data = simplexml_load_string($body, 'SimpleXMLElement', LIBXML_NOCDATA);
$json_data = json_encode($data);
$array_data = json_decode($json_data, true);
$items = [];
foreach ($array_data['channel']['item'] as $item) {
$items[] = [
'title' => $item['title'],
'link' => $item['link'],
'pubDate' => $item['pubDate'],
'description' => $item['description'],
];
}
"Description" is ambiguous: it may be a full post body or just an excerpt. In the block's Edit component, the value is spliced and trimmed to no more than 50 words. Because the returned HTML is rendered as-is, there is some security risk, plus the chance of rendering an open tag without its closing counterpart and breaking the layout. Libraries exist to handle this, but the implementation deliberately keeps things simple.
Storing the Result
Once the items are fetched and parsed, the transient is set:
set_transient($transient_key, $items, 12 * HOUR_IN_SECONDS);
The set_transient() function takes three arguments. The first is the $transient_key named earlier, identifying which transient is being written. The other two:
$value: the object being stored under that key — here, the$itemsobject produced by parsing.$expiration: the lifetime in seconds. This one lasts 12 hours before expiring, after which it refreshes the next time a visitor loads the page.
Finally, the items are returned from the REST API as a new response:
return new WP_REST_Response($items, 200);
A Custom Endpoint
Setting and getting the transient was only half the job. Calling the data requires a custom REST API endpoint, built largely by leaning on the WordPress documentation:
add_action('rest_api_init', function () {
register_rest_route('custom/v1', '/fetch-data', [
'methods' => 'GET',
'callback' => 'fetch_and_store_data',
]);
});
This endpoint was the most time-consuming part, along with wrestling the block itself. Keeping the front- and back-end components in sync is difficult, and much of that code reads as redundant when looked at from a distance.
The output is available as a page pulling in the 10 most recent items, alongside a link to subscribe to the full feed.



