# 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.

 1 2 /*  3 Plugin Name: Adam's random quote  4 Version: 0.1  5 Plugin URI: https://burucs.com  6 Description: Replaces the site's description with a random quote  7 Author: Adam Burucs  8 Author URI: http://burucs.com  9 */ 

## 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 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.

 1 2 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.

 1 2 /*  3 Plugin Name: Adam's random quote  4 Version: 0.1  5 Plugin URI: http://burucs.com  6 Description: Replaces the site's description with a random quote  7 Author: Adam Burucs  8 Author URI: http://burucs.com  9 */  10 11 function ab_arq_generate() {  12  $quotes = array(  13  'Silence is golden.',  14  'Nothing is what it looks like.',  15  'Make everything right!',  16  'Better late than never.',  17  'Seeing is believing.'  18  );  19   20  return$quotes[rand(0, count($quotes)-1)];  21 }  22 23 function ab_arq_change_bloginfo($text, $show ) {  24  if( 'description' ==$show ) {  25  $text = ab_arq_generate();  26  }  27  return$text;  28 }  29 30 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.