OpenBook Plugin: Hooking into WordPress
- When You Link to a Book, Do You Link to Amazon, LibraryThing, WorldCat, or What?
- Four Big Libraries: I Pick OpenLibrary
- OpenBook Plugin: Requirements
- OpenBook Plugin: The Open Library API
- OpenBook Plugin: Coding in PHP
- OpenBook Plugin: Grabbing the Book Data
- OpenBook Plugin: Let There Be Cover Images!
- OpenBook Plugin: The Importance of Linking to Publisher Websites
- OpenBook Plugin: Graphic Design
- OpenBook Plugin: Hooking into WordPress
- OpenBook Plugin: Now Accepting Arguments, and Beta Testers
- OpenBook Beta Release!
<== if you see a sample to the left that's the OpenBook plugin working
We’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.,
{“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.



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.
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!
Slow Reading
Available from Litwin Books | Read a chapter online
OpenBook WordPress Plugin
Inserts a book cover image, title, author, and other book data from Open Library into a WordPress post.
Download the Plugin | Read More | Report an Issue | Join the Discussion List
Series
Other posts belonging to this series
What I’m Reading
Also Reading