Output of the RSS feed of the second site via shortcodes in WordPress

Output of the RSS feed of the second site via shortcodes in WordPress

Greetings everyone! In this article, I’d like to show you how to use shortcodes to connect to an external site, fetch a list of latest posts via RSS, and display them in a nicely formatted block on your own site.

Honestly, the task might seem odd — why would I want links to other sites in my blog? But it’s just an example. You could also fetch exchange rates, weather forecasts for today or the week (which I’ll cover in the next post). For now, let’s stick to reading RSS and displaying the content.

Let’s begin. Open the functions.php file of your theme and add the following code to the end:

function wp2fl_rss($atts, $content = null)
{ 
$atts = shortcode_atts(array( 
'rss' => FALSE, 
),$atts); 

if($atts['rss'] == FALSE) 
{ 
return NULL; 
} 

try 
{ 
$ch = curl_init(); 
curl_setopt($ch,CURLOPT_URL,$atts['rss']); 
curl_setopt($ch,CURLOPT_TIMEOUT,30); 
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE); 
curl_setopt($ch, CURLOPT_FRESH_CONNECT,TRUE); 
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,TRUE); 
curl_setopt($ch,CURLOPT_HEADER,FALSE); 
curl_setopt($ch,CURLOPT_NOBODY,FALSE); 

$xml_data = curl_exec($ch); 
$info_ar = curl_getinfo($ch); 

if($info_ar['http_code'] != 200) 
{ 
return 'Error connecting RSS feed'; 
} 

$XML = new SimpleXMLElement($xml_data); 
$ItemAr = $XML -> channel -> item; 

$c = ''; 
if(sizeof($ItemAr) > 0) 
{ 
$c .= '<div class="wp2fl-list">'; 
if($content == TRUE) 
{ 
$c .= '<h3>'.$content.'</h3>'; 
}
$c .= '<ul>';
foreach($ItemAr as $Item)
{
$c .= '<li>';
$c .= '<a href="'.$Item -> link.'" target="_blank">'.$Item -> title.'</a>';
$c .= '</li>';
}
$c .= '</ul>';
$c .= '</div>';
}
}
catch(Exception $e)
{
return 'An error occurred while processing the RSS feed';
}
return $c;
}

add_shortcode("showrss", "wp2fl_rss");

To use this shortcode, place the following in your post content:

[showrss rss="http://feeds.feedburner.com/wp2fl"]WordPress Tutorials with Examples[/showrss]

Here, rss is the feed URL to display, and the text between the tags is the title of the link block.

Let’s break down how it works

$atts = shortcode_atts(array(
	'rss' => FALSE,
),$atts);

if($atts['rss'] == FALSE)
{
	return NULL;
}

This part sets default values for the showrss shortcode and checks whether the rss attribute is provided. If not — return nothing.

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$atts['rss']);
curl_setopt($ch,CURLOPT_TIMEOUT,30);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch, CURLOPT_FRESH_CONNECT,TRUE);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,TRUE);
curl_setopt($ch,CURLOPT_HEADER,FALSE);
curl_setopt($ch,CURLOPT_NOBODY,FALSE);

$xml_data = curl_exec($ch);
$info_ar = curl_getinfo($ch);

This is the “core” of the script — we make a CURL request to the external site and retrieve the raw XML data. You can read more about CURL in any PHP documentation.

if($info_ar['http_code'] != 200)
{ 
return 'Error connecting RSS feed';
}

We check the HTTP status. If it’s not 200, we show an error. Note: we don’t check the content type (like XML), so if the result isn’t RSS — parsing will fail.

$XML = new SimpleXMLElement($xml_data);
$ItemAr = $XML -> channel -> item;

Here we use SimpleXML to convert the RSS response into an object.

$c = '';
if(sizeof($ItemAr) > 0)
{
	$c .= '<div class="wp2fl-list">';
	if($content == TRUE)
	{
		$c .= '<h3>'.$content.'</h3>';
	}
	$c .= '<ul>';
	foreach($ItemAr as $Item)
	{
		$c .= '<li>';
		$c .= '<a href="'.$Item -> link.'" target="_blank">'.$Item -> title.'</a>';
		$c .= '</li>';
	}
	$c .= '</ul>';
	$c .= '</div>';
}

Then we build an HTML block containing the list of links, and optionally — a title if specified inside the shortcode.

Example output:

List from another site via RSS

That’s it. Have a great weekend 🙂

Posts on similar topics

Are you having problems with your WordPress site? Do you need additional functionality? A custom plugin or a new page?
Then write to me via the feedback form, and I will try to help you.

Write a comment

Your email address will not be published. Required fields are marked *