Code Bytes
Show a small, safe list of links from an RSS feed
Use WordPress's feed parser and cache, handle unavailable feeds, and escape external titles and links before rendering them.
A short “latest articles” list does not need a custom XML scraper. Inside WordPress, use its feed API so parsing, caching and errors follow the platform's existing behavior.
A fixed feed, not a public URL-fetching service
Use this in a server-rendered WordPress template or plugin callback after WordPress has loaded. The feed and permitted article hostname are developer-controlled. This example displays at most three links from the WordPress news feed.
<?php
require_once ABSPATH . WPINC . '/feed.php';
$feed = fetch_feed('https://wordpress.org/news/feed/');
if (is_wp_error($feed)) {
echo '<p>The latest articles are unavailable right now.</p>';
return;
}
echo '<ul>';
foreach ($feed->get_items(0, 3) as $item) {
$url = (string) $item->get_permalink();
$host = wp_parse_url($url, PHP_URL_HOST);
if (wp_parse_url($url, PHP_URL_SCHEME) !== 'https'
|| !is_string($host) || strtolower($host) !== 'wordpress.org') {
continue;
}
$title = wp_strip_all_tags((string) $item->get_title());
echo '<li><a href="' . esc_url($url) . '">'
. esc_html($title) . '</a></li>';
}
echo '</ul>';
Change both the feed URL and the allowed article hostname for another publisher. Avoid printing feed descriptions as trusted HTML. A feed is external input even when you know the publisher.
Keep the work off the critical path
WordPress caches fetch_feed() results; do not disable that cache just to make the list appear more current. Busy or latency-sensitive sites can refresh a saved public projection in a background job instead.
Never replace the fixed URL with an unchecked $_GET['url']. That turns a display feature into a server-side request-forgery risk. Outside WordPress, use a maintained RSS parser with bounded downloads, timeouts and an explicit host policy—not DOMDocument::load() pointed at visitor input.
References: fetch_feed() and WordPress's safe HTTP requests.
Original version4 August 2016
Kept here for reference and earlier links. The updated guide above is the recommended starting point; older code may depend on retired services or different software versions.
Wordpress handily provides a RSS feed, usually at /feed/
This is accessible externally so it's easy to use this data on external or local sites for simple link lists, scraping etc.
Here's a really simple way to generate a li list of article headings along with their relevant link from an external Wordpress feed.
Just replace the URL within the $feedURL variable to your desired feed.
<ul>
<?php
$feedURL = 'https://guwii.com/news/feed/';
$rss = new DOMDocument();
$rss->load($feedURL);
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 3;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
echo '<li><a href="'.$link.'" title="'.$title.'">'.$title.'</a></li>';
}
?>
</ul>Keep exploring