Anatomy of a WordPress Plugin

Watch out! This tutorial is over 5 years old. Please keep this in mind as some code snippets provided may no longer work or need modification to work on current systems.
Tutorial Difficulty Level    

To learn the anatomy of a WordPress plugin and how plugin works, we will start looking at a simply plugin, a plugin that comes pre-install with WordPress.  It is the “Hello Dolly” plugin.  In fact this plugin is there to show you an example of how to do a WordPress plugin.

First let’s see how it work.  In WordPress Dashboard, go to “Plugins -> Installed Plugins” and make sure that the Hello Dolly plugin is activated.  If not, click the “Activate” link.  If you don’t see the “Activate” link and see the “Deactivate” link instead, that means that the plugin is activated.

activate plugin

activate plugin

You should get the message “Plugin Activated” and you will start seeing a random Hello Dolly quote in the upper-right corner of your WordPress dashboard….

hello dolly quotes

hello dolly quotes

Don’t worry, the Hello Dolly plugin only affects the WordPress admin backend.  If you look at the front pages of the site that your user sees, there is no change.

Now let’s see how this WordPress Plugin Works

We open up the hood to look at the code by clicking on the “Edit” link in the Plugin Listing page (shown above).

You get a code editor showing the code for a file name “hello.php”. …

plugin code

plugin code

This plugin is fairly simply so it consists of just this one PHP file.  This hello.php file actually sits in /wp-content/plugins/hello.php if you look on your server.  Although this plugin is not within its own folder, you will find that almost all other plugins will be within its own plugin sub-folder under the plugins directory.

The code start with the PHP start tag.  The PHP ending tag is optional as long as the file ends in PHP code.  In fact, it is preferable to leave it out.  If you do have an ending PHP tag, make sure nothing after it.

Anyways, the top of the hello.php file start off with comments beggining with the header block.  This is the PHPDoc file header block used to give an overview of what is contained in …


The WordPress Documentation handbook states…

“Whenever possible, all WordPress files should contain a header block, regardless of the file’s contents – this includes files containing classes.”

Next comes the plugin documentation comments.  What you put here is what will be shown in the plugin page …

wordpress plugin documentation

wordpress plugin documentation

See how that works?

“Plugin Name” declares the plugin name and is used by WordPress (among other things) to show it on the Plugins page.

Plugin URL is where you can find the plugin download link

Description is the description shown in the plugin page.  You can give brief user instructions here.

Author tells who created the plugin, also shown in the Plugins page

Version tells plugin version, also shown in the Plugins page but also for determining updates

Author URI is the URI of the Author, generally used to supply an URL where the author can be contacted.  It is the link destination of the author link shown in the plugin page.

Register WordPress Plugin Hooks

At the bottom of the file is where the action start with this code …

add_action( ‘admin_head’, ‘dolly_css’ );

The “add_action” call is known as an WordPress action hook. This line of code is known as registering hooks.    Learn more about Action hooks.  “admin_head” is the specific action hook that is being called.  You can look it up on the WordPress Codex.   It has to be spelled exactly like this.  It is not an arbitrary name.

The codex states that …

"admin_head is an action event and can be hooked by add_action hook.”

Whenever the <head> of an admin page loads (which is whenever the page loads), this hook is triggered and the “dolly_css” function is called.  dolly_css is an arbitrarily name function which is defined higher up in the file…

This function adds some embedded CSS to the page.  This declares the style for the lyrics of the Hello Dolly quote.  The is_rtl() checks if the site is RTL (right to left language) or LTR (left to right language) and adapts the styles.  The echo statement generates the dynamic styles used for the paragraph with the lyric.

But how does the Hello Dolly quote get displayed?  It is through another action hook …

This hooks into the admin_notices event which according to the WordPress Codex is for…

“Notices displayed near the top of admin pages”

This hook registers the text generating function to be called on the admin panel.  The hook triggers the function hello_dolly which is our function used to display the lyric.

The hello_dolly function is also defined in hello.php as the following …

The hello_dolly display the lyrics by calling hello_dolly_get_lyrics() which return a random lyric.  The returned lyric is displayed  inside a paragraph with the echo statement.

The hello_dolly_get_lyrics() function has all the lines of the lyrics by stores all the lyrics in an string variable called $lyrics with each line of the lyrics separated by a “\n” newline character …

By splitting at the newline “\n”, the explode function converts the $lyrics variable into an array, with each element of the array containing one line of the lyrics.

mt_rand is used to return a random line from the $lyrics array.  And wptexturize transforms quotes to smart quotes, apostrophes, dashes, ellipses, the trademark symbol, etc.

The plugin is a good starting place to learn.  Plugins can definitely get much more complex.