How to Create a WordPress Plugin

plgWordPress plugins are easy to create even if you’re a newbie in internet and barely know how to work with php. In this tutorial I am going to show you how you can create a simple wordpress plugin, that shows wisdom quotes on the top of your blog. This is very basic tutorial for creating plugin, what shown in this theme can be achieved by other means, but we’re making this as a plugin for a simplicity in learning.

Requirements:

  • WordPress 3.3
  • Text editor
  • php concepts

Random Text Code in Php

We’re going to use a simple php script that lets you randomize the text snippets with each refresh.

 < ?
$random_text = array("Memory is the mother of all wisdom.",
                    "A short saying often contains much wisdom.",
                    "Without courage, wisdom bears no fruit.",
                    "Wisdom is the supreme part of happiness.",
                    "It is characteristic of wisdom not to do desperate things.");
srand(time());
$sizeof = count($random_text);
$random = (rand()%$sizeof);
print("$random_text[$random]");
 
?>

This code will be used in our plugin so that whenever we call the plugin function in our footer or header, it shows the random quotes.

Preparation of Plugin

Create a folder named “Wisdom Footer” and make a file named wisdom-footer.php in it. Now open that php file and add the following code into it.

< ? php
 
/*
Plugin Name: Wisdom Footer
Plugin URI: http://localhost/wordpress/
Description: Adds Wisdom Quotes in footer.
It is a plugin tutorial using latest plugin API of WP3.3+
Version: 0.1
Author: coders
Author URI: http://thecoders.vn
Disclaimer: Use at your own risk. No warranty expressed or implied is provided.
*/
 
? >

Save the file. Now let’s write the function random_quotes so that we can use it later in our footer or index.php file.

Update the wisdom-footer.php with following code.

< ? php
/*
Plugin Name: Wisdom Footer
Plugin URI: http://localhost/wordpress/
Description: Adds Wisdom Quotes in footer.
It is a plugin tutorial using latest plugin API of WP3.3+
Version: 0.1
Author: coders
Author URI: http://thecoders.vn
Disclaimer: Use at your own risk. No warranty expressed or implied is provided.
*/
 
function random_quotes(){
$random_text = array("Memory is the mother of all wisdom.",
                    "A short saying often contains much wisdom.",
                    "Without courage, wisdom bears no fruit.",
                    "Wisdom is the supreme part of happiness.",
                    "It is characteristic of wisdom not to do desperate things.");
srand(time());
$sizeof = count($random_text);
$random = (rand()%$sizeof);
print("$random_text[$random]");
}
? >

Save the file and now find the theme’s index.php or footer.php. For a demo purpose i am using the index.php of twentyeleven theme. I have inserted the following code in the theme’s index.php file.

if(function_exists('random_quotes'))
 {
random_quotes();
 }

Make sure you use this code in the right place or it’ll not call the function “random_quotes”. I suggest placing this code at the top of the index.php so that you can test at the start of loading the theme.

Now pack your themes folder into wp-content/plugins directory and then go into the dashboard and enabled the plugin. Now you’ll see the random wisdom quotes every time you refresh the page.

abu murad
Abu Murad , is fascinated by the constantly changing, complex, layered world of SEO, & content marketing’s ability to reach potential buyers on their terms, in their timing, and in the ways that are most relevant to them.

Leave a Comment