Using WordPress ‘fetch_rss()’ PHP function

The fetch_rss() WordPress PHP function builds a Magpie object based on the RSS feed from a given URL.

Usage

To use fetch_rss(), provide the URL of the RSS feed as a string. The function will then return a Magpie RSS object.

$rss = fetch_rss('https://example.com/rssfeed.xml');

In this example, the function is retrieving the RSS feed from ‘https://example.com/rssfeed.xml‘.

Parameters

  • $url (string): This is a required parameter which specifies the URL from which the RSS feed will be retrieved.

More information

See WordPress Developer Resources: fetch_rss
Please note that as of WordPress 3.0.0, this function has been deprecated. It is recommended to use fetch_feed() instead.

Examples

Basic Use

This code retrieves the RSS feed from a specified URL and stores it in the $rss variable.

$rss = fetch_rss('https://example.com/rssfeed.xml');

Display Titles of RSS Feed Items

This code retrieves the RSS feed and loops through it to display the title of each item.

$rss = fetch_rss('https://example.com/rssfeed.xml');

foreach ($rss->items as $item) {
    $title = $item['title'];
    echo $title . "\n";
}

This code fetches the RSS feed and displays the link of each item.

$rss = fetch_rss('https://example.com/rssfeed.xml');

foreach ($rss->items as $item) {
    $link = $item['link'];
    echo $link . "\n";
}

Display Descriptions of RSS Feed Items

This code retrieves the RSS feed and prints out the description of each item.

$rss = fetch_rss('https://example.com/rssfeed.xml');

foreach ($rss->items as $item) {
    $description = $item['description'];
    echo $description . "\n";
}

Display the Feed Title

This code retrieves the RSS feed and prints out the feed’s title.

$rss = fetch_rss('https://example.com/rssfeed.xml');

$feedTitle = $rss->channel['title'];
echo $feedTitle . "\n";
}

Please remember that these examples are using a deprecated function and you should consider using fetch_feed() for future WordPress development.