Advertisement

Creating a Random Quote Plugin for WordPress

by
Student iconAre you a student? Get a yearly Tuts+ subscription for $45 →

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.

/*
Plugin Name: Adam's random quote
Version: 0.1
Plugin URI: http://burucs.com
Description: Replaces the site's description with a random quote
Author: Adam Burucs
Author URI: http://burucs.com
*/

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.

function ab_arq_generate() {
    $quotes = array(
        'Silence is golden.',
        'Nothing is what it looks like.',
        'Make everything right!',
        'Better late than never.',
        'Seeing is believing.'
    );
    
    return $quotes[rand(0, count($quotes)-1)];
}

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.

function ab_arq_change_bloginfo( $text, $show ) {
    if( 'description' == $show ) {
        $text = ab_arq_generate();
    }
    return $text;
}

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.

add_filter( 'bloginfo', 'ab_arq_change_bloginfo', 10, 2 );

Step 5 Final code

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

/*
Plugin Name: Adam's random quote
Version: 0.1
Plugin URI: http://burucs.com
Description: Replaces the site's description with a random quote
Author: Adam Burucs
Author URI: http://burucs.com
*/

function ab_arq_generate() {
    $quotes = array(
        'Silence is golden.',
        'Nothing is what it looks like.',
        'Make everything right!',
        'Better late than never.',
        'Seeing is believing.'
    );
    
    return $quotes[rand(0, count($quotes)-1)];
}

function ab_arq_change_bloginfo( $text, $show ) {
    if( 'description' == $show ) {
        $text = ab_arq_generate();
    }
    return $text;
}

add_filter( 'bloginfo', 'ab_arq_change_bloginfo', 10, 2 );

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.

Advertisement