Creating a Random Quote Plugin for WordPress


A few years back I was just really getting into WordPress plugin development when I stumbled upon an outstanding little exercise that taught me a lot of the basics of plugin creation. I started with this very simple idea: creating a handy little plugin which generates a quote randomly to the description of the current theme. Today, I'm going to revisit the plugin that really helped me get my feet wet by walking you guys/gals through how to do it yourself.

The minimum required knowledge for this tutorial is just some basic knowledge of PHP (this is intended for beginners who want to jump into plugin development). The mission for this lesson: create a plugin that will generate a quote randomly each time the page is reloaded. When the plugin is activated in the Plugins section it will function silently. It does not create any resource so we do not need the uninstall procedure.

Step 1 Basic Plugin Data

The following lines of code are essentially what tells WordPress that what you are building is a "plugin". WordPress basically just needs the Plugin Name. The order of the lines is not important, but you'll notice a lot of similarities in this "header" if you've ever created a page template or a theme.

The file must be in UTF-8 encoding.

Step 2 The Generator Function

Our first functional piece of code is the generator function. It consists of a local, one dimensional array, filled with the various quotes and at the end of the routine we select one quote randomly. We use the built-in PHP rand function for this, note that the start and end values are inclusive in the generated range.

According to the detailed WordPress coding standards we should use the following default naming convention to use: two small letter monogram of the author and the abbreviation of the plugin name.

Step 3 Function that replaces the default description to the quote

In this section we use a helper function which utilizes the core procedure written previously.

Step 4 Use the filtering system

The filtering system is an important concept in the monumental world of WordPress.Filters are the functions that your plugin can hook into with regards to modifying data. In our case, with this great potential, we override the default description of the actual theme. Underway we have to use the bloginfo function.

Step 5 Final code

Here is the end product of what we have done in this easy lesson.

Step 6 Bonus Tip

So far I've shown you the basics for creating this plugin, but you could extend it in a few ways if you wanted. For instance: What if we have to deal with more quotes? We can choose from several options. First: we could make an additional php file with the array to be included in the plugin. Second: using a dedicated database table. Third: retrieving data from a remote server (some more possibilities to develop).

Note that if you choose to do the dedicated table version then you should create a good, separate uninstaller function in your plugin!

As today we just wanted to cover the basics though, I'll stop here and direct you to another great "Plugin Development Introduction" article from Tom McFarlin.