FOAF output from WordPress

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, /profile2.php.

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 /index.php:

<?php _e("Filed under:"); ?> <?php the_category() ?> &#8212;
  <?php the_author_posts_link(); ?> 
  @ <?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 /index.php):

<div id="content">
<?php if ('' != $author): ?> 
  <h2>Author profile for <?php the_author(); ?></h2> 
  <div class="profile"> 
    <?php the_profile(); ?> 
  </div>
<?php endif; ?>
<?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 muse, crush, date, and sweetheart, so these are simply ignored in the translation process. Also, the relationship terms childOf and 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');
if ('' != $author) { 
  $authordata = get_userdata($author); 
  require('./wp-profile.php');
}
?>

The final step is adding a link element, for FOAF autodiscovery, to the head of the HTML profile page, also in /index.php:

<link rel="alternate" type="application/rdf+xml" title="RSS 1.0" href="<?php bloginfo('rdf_url'); ?>" /> 
  <?php if ('' != $author): ?> 
    <link rel="meta" type="application/rdf+xml" title="FOAF" href="<?php print get_author_link(0, $authordata->ID, $authordata->user_nicename); ?>?format=rdf" /> 
<?php endif; ?>

Phew, that should do it, see for yourself on my profile page.

Next on the agenda might be integrating with a triple store…

7 thoughts on “FOAF output from WordPress

  1. Morten – Interesting additions to WP. I added the FOAF profile mods to my WP install and all went smoothly. A suggestion: in the documentation’s code, there are several smart single quotes. They will cause errors if copied and used directly. Thanks for sharing your work/ideas.

  2. Hi! Thanks for this, it’s really helpful, one thing though, you have a link to a foaf.rdf file on your profile, however, I could not get wp to generate this, (have tried playing with .htaccess :D )

    Could you tell us how???

    Cheers :D

Comments are closed.