Dougal Campbell's geek ramblings

WordPress, web development, and world domination.

Review: WordPress 3 Plugin Development Essentials

I recently received a copy of WordPress 3 Plugin Development Essentials from Packt Publishing. This book was authored by Brian Bondari and Everett Griffiths, who run the TipsFor.us site together. I also noted that one of the technical editors was Paul Thewlis, who wrote WordPress for Business Bloggers, which I reviewed here previously.

I’m always eager to check out new programming books that come my way, and that’s especially true for books that cover WordPress plugins. There are certain styles for programming books that you come to recognize — a sort of Pattern. And there are particular patterns that annoy me a little. But this book does not use those patterns, so it was refreshing to see a teaching approach that I actually like. I have to say that overall I’m pleased with this one, though there were a couple of negatives that jumped out at me a little. But first, let’s cover the good points.

The first chapter of WordPress 3 Plugin Development Essentials is pretty typical stuff for a development book. It covers some basic information about how to get a web development stack for your computer (MAMP, WAMP, XAMPP, Microsoft Web Platform, etc.), text editors and IDEs, etc. You’ll also find brief descriptions of WordPress, what plugins are, some information about source control and debugging, and a few other tidbits.

Chapter 2 is where the meat begins. Matt Mullenweg will appreciate that this book begins with a dissection of the infamous Hello Dolly plugin. The authors use the existing plugin code to illustrate the core concepts of a plugin: the information header, user-defined functions, and WordPress API hooks. But what I really liked about this first chapter is that the first exercise for the reader is to break the plugin. In fact, they have you break the plugin in several different ways, each illustrating different ways that you might encounter problems when developing your own plugin code.

First, they have you remove the plugin header, and observe how WordPress will notify about automatically deactivating the plugin. Then they have you insert gibberish into the plugin to show what happens when a fatal syntax error occurs. Then they have you send output to the browser without going through WordPress hooks, showing the PHP warning about sending output before HTTP headers are sent. And they also show the result of having whitespace occur after the final PHP closing ?> tag, and how it is safe to omit it to avoid the error. And, of course, they also go through modifying Hello Dolly by changing which hooks it calls, so that it sends output to post content on the front page, instead of in the admin pages.

I thought that this was an excellent way to start the process. Too often, programming books give you the canonical “Hello, World” example, which doesn’t do much for you in the way of learning something new. In this case, they take an existing variation of Hello, World, and show you how changing various pieces of it affect WordPress or how the plugin itself operates. Exposing the reader to errors deliberately, and explaining why they happened, will help the user understand and recover from errors later on, when they happen accidentally. Kudos for that!

The next chapter goes more in-depth, creating a “Digg This” plugin from scratch. This task is simple enough to understand pretty easily, while covering key concepts which help the reader become more familiar with several important WordPress functions and hooks. This process covers fetching a post’s title, content, categories and URL, as well as inserting Javascript into the page head. They start by creating an empty skeleton with stub functions for each method the plugin needs, and fill in the functions one-by-one, explaining its purpose.

I won’t detail every chapter, but here’s a quick synopsis:

  1. Preparing for WordPress Development, an introduction to WordPress and setting up a development environment
  2. Anatomy of a Plugin, using Hello Dolly as an example
  3. Social Bookmarking, building a Digg This button plugin
  4. Ajax Search plugin, implementing “live search” using jQuery, and introducing using PHP classes to encapsulate plugin functions
  5. Content Rotator plugin, covers extending the WP_Widget class and adding a custom plugin settings page
  6. Standardized Custom Content, working with Custom Fields (post meta), custom meta boxes, creating and modifying a child theme
  7. Custom Post Types, working with Custom Post Types, using the Shortcode API, adding a settings shortcut link to the plugins page, plugin uninstall handling
  8. Versioning Your Code with Subversion (SVN), introduction to using svn to manage your code revisions
  9. Preparing Your Plugin for Distribution, cleaning up your code, testing
  10. Publishing Your Plugin, internationalization and localization, readme.txt file format, submitting your plugin to the wordpress.org repository

There are also two appendices. The first is a list of helpful online resources such as the PHP documentation site, the WordPress Codex, and a handful of other WordPress and web development sites. Appendix B is a list of WordPress API functions used in the book, with a brief description of their usage.

While overall I felt like this book takes a good approach and paces things well, I do have a couple of nits to pick, particularly with chapters 4 and 5. When publishing a book like this, I feel it is very important to educate the reader using best practices. Sometimes you might need to illustrate “dirty” ways of doing things to make a point, or to ease readers into a complex subject, but you should always point out best practices. For the most part, this book did a pretty good job of that, and I was pleased to see some discussion of important topics like escaping data to avoid cross-site scripting problems and other similar security risks, for example. But in the discussions of Ajax and creating plugin option pages, they fell down a bit.

First, while I felt like a live search plugin was a useful example to use for discussing Ajax, I was disappointed to see that the authors did not use the built-in WordPress AJAX APIs for this. The wp_ajax_* and wp_ajax_nopriv_* functions really make this fairly easy, once you wrap your head around it.

And second, the examples of creating option pages did not take advantage of the Settings API. Using this API has several advantages over building your own form manually: It automatically handles security nonces for you, it automatically handles saving options to the database as a serialized array, it lets you create a validation callback function to error-check the data, it gives your options page user interface consistency with the standard WordPress admin, and it future-proofs your options page against changes to the standard UI. I’d also recommend checking out Otto’s Settings API Tutorial for some clear examples.

Other than those two exceptions, I felt like this book provided useful examples which gently introduce the reader to important WordPress plugin API concepts. As the book progresses, it builds upon concepts learned previously, while providing a variety of individual example plugin projects. This is another point that I really liked — variety. I don’t like books that take the approach of building a single, large application (“let’s build a shopping cart!” or “let’s build a product catalog site!“). By building several stand-alone plugins to illustrate each chapter’s concepts, the reader is more likely to find something that piques their interest, and it becomes more digestible.

You can get more details and download a sample chapter (Chapter 5: Content Rotator) from the Packt Publishing site. Or, Buy from Amazon.com.

[az_easel item=”184951352X”]

Disclosure: Amazon product links are affiliate links, which provide a small sales commission to me, and help support this site. Packt Publishing provided me with a free copy of this book to review, but I make every effort to keep my reviews unbiased and honest.

 

About Dougal Campbell

Dougal is a web developer, and a "Developer Emeritus" for the WordPress platform. When he's not coding PHP, Perl, CSS, JavaScript, or whatnot, he spends time with his wife, three children, a dog, and a cat in their Atlanta area home.
This entry was posted in WordPress and tagged , , , , . Bookmark the permalink.

One Response to Review: WordPress 3 Plugin Development Essentials

Leave a Reply