putting together a plugin…
Well I scripted up some quick code in the sidebar to do a blogroll derived from Blogline’s info. It’s working nicely, and it occurred to me that I should try to turn it into a plugin for the exercise.
Interestingly enough, when I googled for a WordPress plugin using Bloglines, I didn’t find anything that created a blogroll from Bloglines (no doubt since WP has its own blogrolling facilities). What I did find were utilities that would let the reader add the feed into their Blogline account that sort of thing.
Checking around, Bloglines does have an API, although I did not find a PHP hook into it, there are Perl, etc modules. But, there’s this http call that PHP can use, which is what makes up the basis of my plugin.
So the initial cut of the code, directly into my sidebar, was as follows:
[code lang=”php”]
-
$ch = curl_init();
- “.htmlspecialchars($line).”
\n”;
if (ereg(’http’, $line) &&
!ereg(’Powered’, $line)) {
echo “ - $line\n”;
}
}
?>
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, 'http://rpc.bloglines.com/blogroll?html=1&id=digitalramble');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_e x e c ($ch);
curl_close($ch);
$lines = array();
$lines = explode("\n", $file_contents);
// display file line by line
foreach($lines as $line_num => $line) {
//echo “
[/code]
Since Bloglines is kind enough to provide access to shared links (see their share information), I made use of that in the code above. I did check around their API documentation and there’s modules for Perl, Ruby, and Python, plus a number of various widgets and plugins, but oddly enough none for WordPress.
Now, to make it into a plugin. First I peek at a couple of other plugins that I have, to see the general format and I mock up my initial file:
[code lang=”php”]
/*
Plugin Name: Bloglines Blogroll
Plugin URI: http://www.digitalramble.com/
Description: Uses your bloglines account to create a blogroll in your sidebar.
Author: Cindy Moore
Author URI: http://digitalramble.com
Version: 1.0
Disclaimer: Use at your own risk. No warranty expressed or implied is provided.
*/
[/code]
All the plugins start out with this kind of header, and this info is used when you add a plugin to your plugins directory then look on your Dashboard/Plugins to see what you can activate or deactivate. Obviously once I get this working later on I'll put together a page for BloglinesBlogroll’s very own.
Next step is to decide on the parameters of the function call. If you go back to Blogline’s Share information, it says
Use the following wizard to generate the HTML for your blog. If you leave the Folder field blank, all of your public subscriptions will be displayed. If you leave the Link Target field blank, links will load in same window as the blogroll.
So it will need three pieces of information, two of which are optional. The required bit is the public Bloglines username. The two optional are the folder and the target information. Because this is plugging into the sidebar, we’ll also need a title. So putting that together, the next section is:
[code lang=”php”]
function printBloglinesBlogroll($title, $id, $folder=”, $target=”)
[/code]
So now from this information, construct the calling URI that Bloglines will expect:
[code lang=”php”]
$blprefix = ‘http://rpc.bloglines.com/blogroll?html=1&id=’;
$blurl = $blprefix.$id;
if (strlen($folder)>0)
{
$blurl .= ‘&folder=’.$folder;
}
if (strlen($target)>0)
{
$blurl .= ‘&target=’.$target;
}
[/code]
OK, so now we’re ready to output. Instead of just printing as I go, I’ve chosen to collect the output into a variable for printing at the end. (If later on I want to modify that output before printing it, I can do so with minimum trouble.)
[code lang=”php”]
$output .= “
$output .= $title.”\n”;
$output .= “
- \n”;
- $line
[/code]
I had to play around a little bit with this. PHP treats single quotes/apostrophe marks differently from quotes (two marks) and in terms of printable output, ” works better. The
.= is just a catenation function, which says, put this stuff at the end of $output, keeping what’s already in $output safe. I’m wrapping this up in a blogroll class so that it can be styled in the css by the individual if they wish. (I may go back and add more for the individual items, but for now I’ve left it alone.)
Now we’re back to the CURL replacement functions. Since not all places have curl support compiled into them, I’ve added commentary for people to switch things around should they wish to, since I’ll be releasing this as a plugin.
[code lang=”php”]
// safely download contents of external url using CURL
// if no curl support, just replace the next nine lines with this one
// $lines = file(’http://example.com/’);
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $blurl);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_e x e c($ch);
curl_close($ch);
$lines = array();
$lines = explode(”\n”, $file_contents);
// end CURL file() substitute
[/code]
As I’ve noted elsewhere, I cannot quote curl_e x e c() in my posts. At the end of this, $lines is an array of single lines as handed back by Bloglines. This has some extra stuff it in (their own wrapper stuff, which isn’t necessarily set up to go into a WP sidebar) so what I do next is toss out anything that isn’t a simple link:
[code lang=”php”]
// display file line by line
foreach($lines as $line_num => $line)
{
if (ereg(’http’, $line) &&
!ereg(’Powered’, $line))
{
$output .= “
\n”;
}
}
[/code]
You’ll notice how I’ve modified this slightly so that the line is actually just added on to the $output variable. Time to wrap things up:
[code lang=”php”]
$output .= “
\nAcquired from Bloglines\n”;
$output .= “
\n”;
$output .= “
\n”;
[/code]
Close off all the html, etc. And now print it out:
[code lang=”php”]
echo $output;
return;
}
?>
[/code]
At this point I saved it off into a php file and put that in my plugins directory. Going over to Dashboard/Plugins and voila! There it was! My first plugin popped up like a pro! Activating it, though, was interesting. I got an error message that made no real sense, and looking at the file didn’t give me any answers. So googling on the error message, I found this:
And this error:
Warning: Cannot modify header information - headers already sent by (output started at /home/facethem/public_html/wp-content/plugins/wpvideo.php:1)
tells there is a blank space in line 1 of the plugin file. Edit it in a plain text ediotr and check for blank space/line
Interesting. So it’s persnickety about that — I’ll have to remember that cos I’m a big believer in extra white space for readability and so on. In any case, removing the offending lines cleared it up and I was able to activate the plugin and put in the call to my sidebar, and voila!
I’ve tested it as follows: I’ve given it a public folder, and it’s properly retrieved feeds from that. I’ve given it no folder, and put some public feeds inside a public folder, and it retrieved all the feeds and printed them out, without any nesting or lossage of interior feeds (that’s what I wanted, a blogroll really doesn’t need to be anything more complicated than a list of links). I removed feeds, added feeds, set feeds to public and private and back again, and the plugin handled all the cases. It handled being given a private folder, a non existent folder and a public one.
I think for using this, I would recommend creating a single folder and putting all the subscriptions you want to appear in your WP into that folder. That way you can have your own subscriptions which you may want to keep private or at least off your WP because they are off topic, etc.


