Innovate | Connect | Transform
ContactService statusUSD $

GICT knowledge base

Example of writing a simple WordPress Plugin

Published

WordPress

From the moment we start blogging on WordPress we install as many plugins as possible to get the best out of the WordPress platform. For the non coder a single line of code seems an insurmountable mountain but it is not the case. You can make your very own WordPress plugin.  Even though I am no big coder I can show you how to create a simple WordPress plugin. It’s that simple.

  • Plugin Name – When you name your plugin ensure that it’s not identical to any other plugin out there. You can perform this basic step by just heading over to your dashboard and keying in the desired name to see if there exists any plugin with that name. Here I am going to name the Plugin “Notifier”. A quick search shows that there is no plugin with that name. Wow! I am lucky.

Wpwriteplug1

  • Version – You might have seen plugins with frequent updates with the 3 digit version like 2.0.1. In this case since we’re going for a simple one, a two digit version would suffice something like 1.0
  • Description – If you plan to make plugins for public use then this step is important. In simple words you need to explain what your plugin does.
  • Author – The name of the creator
  • Author URI – Your site url.
  • Plugin URI – In this case I would leave it empty. This field indicates the dedicated page or support page for your plugin.
  • License – For WordPress plugins a GPL V2 license would be enough
  • License url- You can link to the page with the information on license

Create a .txt file with and name it as yourname-plugin.txt

Here is the code:

<?php

/* Plugin Name: Notifier

Version: 1.0

Description: It displays a notice on your WordPress dashboard

Author: George Mathew

Author URI: http://URI_of_plugin_author_website

Plugin URI: blank License: usually GPLv2

License url:http://gnu.org/licenses

*/

function display_admin_notice() {

echo ‘<p>This plugin is by Servlet</p>’;

}

add_action(‘admin_notices’, ‘display_admin_notice’);

?>

Step 1: Go to your hosting account and select the WordPress site where you want to create a new plugin. In the site’s file structure you will find a folder under wp-content with the name plugins. Just click on that. On the right side you will see the list of active plugins.

In this case I am using a test site.

Wpwriteplug2

Step 2- Select new file from the top header

 Wpwriteplug3

Step 3: Name it as Yournameplugin.php or anything else you want with the php extension.

 

Wpwriteplug4

 

Step 4: A new file opens up. Copy the code from the notepad file and paste it in here. The functions need to be added just above the ?>.

Wpwriteplug5

Step 4: Go to the WordPress dashboard and go the the plugin list and you will see a new plugin.

 Wpwriteplug6

Step 5: You can always edit the plugin from the Edit option displayed there.

 

Wpwriteplug7

 

Step 6: For the moment let’s just activate the plugin

Wpwriteplug8

As you can see the plugin is activated and you can see a notice “See you have a notice on your dashboard.” You can replace it with anything that you like.

You might have seen some functions in the code. Let’s see what they do.

What does add_action do?

Add_action does the magic to our code. Add_action is the wordpress function that actually helps perform the action specified by the user. You may not realize this but most of the common activities on a WordPress blog employ Add_action

Here are some common examples

  • publish_post – called when a post is published or when status is changed into “published”
  • save_post – called when a post/page is created from start or updated
  • trackback_post – called whenever a new trackback is added into a post

What does display_admin_notice do?

It just displays a message. In our case it’s the notice.

So, we just made our first WordPress plugin. Was it that hard?

Related guides

Account services are unavailable until the production backend is configured.