Note: Please see FOAF Output Plugin for newer versions of WordPress.
A few days ago, Christopher Schmidt mentioned in a comment that he had been hacking on FOAF export for WordPress. He kindly let me take a look at his work, and in addition posted a message to rdfweb-dev.
In short, he had created a set of dynamic stand-alone profile pages, one with HTML output and one with RDF/XML output using FOAF, in a single file,
Seeing that, it dawned on me: WordPress already has a “profile” page for each of the authors, likely at
/archives/author/<login>/ (depending on the permalink structure defined) — and it even has a template name:
the_author_posts_link. In its default incarnation it’s simply a list of the posts by that author, but turning it into a profile page by also showing the basic information and linked friends didn’t seem too hard.
So, first order of business is making the profile page “visible” by linking to it from the author name displayed with each post, by hacking in
<?php _e("Filed under:"); ?> <?php the_category() ?> — <?php the_author(); ?> @ <?php the_time() ?> <?php edit_post_link(); ?>
Next up is actually displaying the profile information when desired — when an author is specified as the search criteria (in
<div id="content"> <?php if ($posts) : foreach ($posts as $post) : start_wp(); ?>
This is not enough however, since the template
the_profile isn’t defined yet. This is done in
wp-profile.php.txt (rename to
wp-profile.php and place in root directory), which is an extended version of Christopher Schmidt’s original.
This new version now also includes relationships based on the XFN link
rel definitions, most of which are also translated into terms from the relationship vocabulary. This last task provided for a few challenges.
First, as has been pointed out by Leigh Dodds in his post simply titled XFN, the information expressed with XFN is incomplete as compared to FOAF and a few assumptions are needed, so if you intend to use this code, make sure you fill out the link fields in the link manager as follows, otherwise the FOAF output will be wrong:
- URI: Link to weblog of the person.
- Link Name: Name of the weblog.
- Short description: Name of the person.
Second, the relationship vocabulary doesn’t have equivalent terms for the XFN relationships
sweetheart, so these are simply ignored in the translation process. Also, the relationship terms
parentOf seems to be incorrectly or at least vaguely specified.
Next up is making sure that the actual FOAF output in RDF/XML is sent when requested. For this to work, a few extra lines at the top of
/index.php are needed:
<?php /* Don't remove this line. */ require('./wp-blog-header.php'); ?>
The final step is adding a link element, for FOAF autodiscovery, to the head of the HTML profile page, also in
<link rel="alternate" type="application/rdf+xml" title="RSS 1.0" href="<?php bloginfo('rdf_url'); ?>" />
Phew, that should do it, see for yourself on my profile page.
Next on the agenda might be integrating with a triple store…