Integrating a WordPress Blogroll into a MODx Sidebar

9 Apr, 2009
4 Comments
Since I could not find a decent article out there to successfully perform this task, I thought I would do it myself in my own style. Now, this can work with any WordPress install that uses RSS (which ones don't?) and a MODx install that has snippets (again, which versions don't?). My intention with this work was to integrate a new WordPress (stand-alone) blog's roll into the sidebar of an existing MODx installation. This combines the power of WordPress as a blogging platform with MODx's power as a Content Management System. Both sites happen to exist on the same domain. In trying to solve this problem, I decided to forego a true integration of WordPress into MODx as this seemed a little overkill for the limited use I was making of WordPress. So, this meant using probably the easiest technology: RSS. After a bit of Googling, I came across an RSS Extractor and Displayer. As this sounded like the ticket, I went to Scriptol.com and downloaded their RSS Reader. I took a look at their instructions, their code, and decided on the RSS_Display() function. All you have to do is pass it the URL and number of listings you would like to display, and it will retrieve them for you. Of note: the first listing for the WordPress RSS feed we set up is a simple title and link back to the blog. As this was rather redundant, I adjusted the code to ignore the first listing. I uploaded the file, 'rsslib.php' to my server in a directory called 'rss'. You can choose whichever directory you would like, just remember it! As well, if you are doing this, make sure it is on the server which has the MODx website! So, in MODx, I created a new snippet called "blog-rss-feed" and inserted:
<?php
	include_once("rss/rsslib.php");

	$output = '<div class="call-feed blog">'.
		  '		<h4>Recent Blog Posts</h4>';

	$output .= RSS_Display("http://www.example.com/rss", 6);
	$output .= '	<a class="view-all" href="/blog/">'.
		   'View All Blog Posts</a></div>';
	return $output;
?>
Now, a bit of an explanation of the code above. The first line is essential. If you do not include the RSS PHP code, your function call will not work. Make sure (again) that you are pointing to the right file! I surrounded the called code with a DIV to help with styling, but feel free to skip this step if you so wish. Same with the title. You will notice that I use the convention of concatenating the output to a variable to an appropriately named variable, '$output'. I do this because a MODx snippet expects a string to be returned at the end. As opposed to using echo() calls all over the place, this will allow for you to do pre- and post-formatting of the output. Always a nice thing. I added a link at the bottom to the location of the actual blog (a nice convention) as well as a DIV, again for styling. I called [[blog-rss-feed]] from within the website template, but you can choose whichever template you would like. For instance, if you only want it on the front page, and have a template for the front page, throw it in there. Of note, I surrounded the snippet call with non-displayed codes so I could troubleshoot the output. Your call. This effectively gave me a blog roll for the website! From another website! Advanced Modifications: Props go out to the peeps at ScripTol.com, but their output was a little less than semantic for our purposes. They surrounded each listing with an unordered list tag, UL, and each part of the listing with a list item, LI, tag. They separate some of the sections with line breaks, BR, and use the bold tag, B, for the first listing. Because we were needing a full semantic treatment, I cut these out and surrounded each listing with its own DIV class, and each title and description with its own. There was a "more info" convention we were using, so I put the "more info" link within its own DIV class as well. What we really wanted it to look like was this:
<div class="call-feed blog">
	<h4>Blog Roll Title</h4>
	<div class="listing">
		<div class="title">
			<a href="/blog/1">The Title</a>
		</div>
		<div class="description">
			Lorem ipsum [...]
		</div>
		<div class="more-info-link">
			<a href="/blog/1">more info&gt;</a>
		</div>
	</div>
	.
	.
	.
</div>
My new rewritten function looks like this:
function RSS_Display($url, $size)
{
	global $RSS_Content;

	$opened = false;
	$page = "";

	RSS_Retrieve($url);
	if($size > 0) {
		$recents = array_slice($RSS_Content, 0, $size);
	}

	foreach($recents as $article)
	{
		$type = $article["type"];
		if($type != 0) {
			$title = $article["title"];
			$link = $article["link"];
			$description = $article["description"];
			$page .= '<div class="'.LISTINGDIV.'">'."\n";
			$page .= '<div class="'.TITLEDIV.'">'.
				 '<a href="'.$link.'">'.
				 $title.'</a>'."</div>\n";
			if($description != false)
			{
				$page .= '<div class="'.DESCDIV.'">'.
				  	  substr($description, 0, SUBSTRSIZE).
					  "[...]</div>\n";
			}
			$page .= '<div class="'.INFODIV.'"><a href="'.$link.'">'.
				 INFOTXT.'</a></div>'."\n";
			$page .= "</div>\n";
		}
	}

	return $page."\n";
}
As you may have noticed, I did not hardcode in the DIV class names (yay!). Instead, I have allowed you to change some define() values at the start of the file. Here is what they look like:
define('SUBSTRSIZE',130);     	     // the max size of the description
define('LISTINGDIV','listing');      // <div> class surrounding listing
define('TITLEDIV','title');	     // <div> class surrounding title
define('DESCDIV','synopsis');	     // <div> class surrounding description
define('INFODIV','more-info-link');  // <div> class surrounding 'more info' link
define('INFOTXT','more &gt;');	     // text for 'more info' link
And after all of that, we have a MODx sidebar that incorporates a WordPress blogroll using RSS! This project is for a customer and their blog has not gone live. Stay tuned for an update with their new blog soon! Apologies for the formatting of the code. Some changes were made to the original code to ensure that the examples fit within the confines of the blog. If these adjustment have broken the code, my condolences. Here is the original file I used: rsslib.php

Comments

Love your blog post but need some help

Hi

Thank you for your blog post, my shared host has blocked all securities in the form of fsockopen for a url ... is there anyway to modify your script that I can call the blog directly via the root instead of a url?

I truly appreciate your help

regards

Lance

Love your blog post but need some help

Hi

Thank you for your blog post, my shared host has blocked all securities in the form of fsockopen for a url ... is there anyway to modify your script that I can call the blog directly via the root instead of a url?

I truly appreciate your help

regards

Lance

Kaspar, I do not know

Kaspar,

I do not know exactly why you are getting the error you mention (as I did not write the RSS-reading code), but it looks like the code of the page you are using this feature on is attempting to use the same attribute name as one used in the RSS code. A simple Google search did not return any meaningful results, so you will likely have to debug the RSS and your own code.

Reed

Hey, when I follow your

Hey, when I follow your instructions then I get error @ http://lawyerpronto.com/testing2

DOMDocument::load() [domdocument.load]: Attribute value redefined in http://lawyerpronto.com/lawblog/feed, line: 37

Post new comment

The content of this field is kept private and will not be shown publicly.