Home » OpenBook WordPress Plugin

OpenBook Plugin: Hooking into WordPress

4 June 2008 2 Comments
This entry is part 10 of 12 in the series Building OpenBook

<== if you see a sample to the left that's the OpenBook plugin working

OpenBook WordPress PluginWe’ve scooped all the required book data from Open Library, processed it in PHP, and know how the results will be formatted. The next step is to put everything into a WordPress plugin.

Life is going to be simple for users of the plugin. All they will have to do is insert tags and an ISBN# (10-digit or 13-digit) into a post, e.g.,

Cluetrain Manifesto
The Cluetrain Manifesto: The End Of Business As Usual
; Perseus Books 2000
WorldCatLibraryThingGoogle BooksBookFinder

. Earlier I showed how OpenLibrary returns book data using its internal IDs. Users shouldn’t have to look up those IDs. The OpenLibrary APIs allow us to lookup the internal IDs by ISBN. For example, the following URL has an ISBN in the querystring and yields the given OpenLibrary internal IDs. Notice there are two results, corresponding to the two records in OpenLibrary. OpenBook will use the first result.

http://openlibrary.org/api/search?q={“query”:”(isbn_10:(0786416084) OR isbn_13:(0786416084))”}&text=true

{“status”: “ok”, “result”: ["\/b\/OL3674869M", "\/b\/OL8138859M"]}

WordPress offers various hooks for plugins. Filters are a type of hook intended to modify post content, and my plan is to replace the tagged ISBN above with the book data, so the first step will be to build a filter function.

Step 1. Build the function.

Here is a pseudo-code version of the filter function, with comments to describe each step.

function openbook_insertbookdata($content) {

//1. the $content of the post is passed to this function
//first check if the post has the open and close tags, if not do nothing

$opentagstart = stripos($content,”

;

“);

if ($opentagstart != false && $closetagstart != false) {

//2. if the tags exist, extract the ISBN and the full tag string so you can work with them

$opentagend = $opentagstart + 9;
$isbnstart = $opentagend + 1;
$isbnlength = ($closetagstart – 1) – ($opentagend + 1);
$isbn = substr($content, $isbnstart, $isbnlength);

$tagstart = $opentagstart;
$closetagend = $closetagstart + 9;
$taglength = $closetagend – $opentagstart + 1;
$tag = substr($content, $tagstart, $taglength);

//3. now that you have the ISBN, you can use it to call OpenLibrary and build everything
//we have already discussed most of that work in the previous posts
//for this sample, I will just insert the words “book data” to stand for everything
$bookdata = “book data”;

//4. you don’t want to see the tags and ISBN in the final post displayed to the user
//so strip it out
$tag = substr($content, $tagstart, $taglength);
$content = str_replace($tag, “”, $content);

//5. insert the book data at the beginning of the content
$content = $bookdata . $content;
}

//6. any modifications to the content will be shown to the user
echo $content;
}

Almost done. We put the function in a file with a php extension, and add one more line of code after the function to hook it in: add_filter(‘the_content’, ‘openbook_insertbookdata’);.

That’s it. We have just built a WordPress plugin. The next step is to test and distribute it. Actually, my next step is to take all the pieces we’ve talked about and code them up properly. Once it’s done, I’ll put out a call for beta testers. Interested? Comment here, or email me at openbook@johnmiedema.ca.

Index

Series Navigation«OpenBook Plugin: Graphic DesignOpenBook Plugin: Now Accepting Arguments, and Beta Testers»

Post to Twitter

2 Comments »

  • Dave said:

    Yes, I’m interested! I’m already using over 20 plugins (it’s an addiction) so there should be multiple opportunities to see if OpenBook plays well with others.

  • John said:

    Great Dave. I coded it up during the hockey game last night, and it looks good so far. I am going to do some further tests, then make an announcement. I will also email those who have stated an interest. Likely mid-next-week. Thanks!

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.