Creating Our First WordPress Plugin

So you finally decided to create your own plugin. But don’t know where to start?
Well in that case I can only say-

“Welcome Aboard’ 🙂

If you have searched on the Internet for plugin development, like I did, you would certainly be over-whelmed with so many tutorials and guides. And you can’t decide which one is better. Let me tell you, all of them are good and useful but certainly not the best. What?! Yes, you read that right. Nothing’s ever perfect, there’s always a need for improvement. Its the law of development.

Before you say anything, this tutorial falls in that same category. What I’ll teach you today is just the basic stuff to get you started in this field of WordPress plugin development.
Anyway, let’s begin with our tutorial. But first, let’s get the definition out of the way.

Plugin: In computing, a plug-in (or plugin) is a software component that adds a specific feature to an existing software application. When an application supports plug-ins, it enables customization.
(Source- Wikipedia)

To write a plugin for WordPress we must follow some guidelines that WordPress has provided. You can read about them here.
Ok! Here’s what we are going to do, we’ll create a plugin named ‘demo’ in this tutorial that will display the content ‘Hello! This is my first Plugin’ in any page or post that we want.

First let’s create a folder named ‘demo’ in our WordPress plugin directory. And then we create a file named ‘demo.php’ in it.
After creating it, we open that file in any text-editor and write the following code.

<?php
/* Plugin Name: demo
Plugin URI: http://demo.wordpress.com
Description: A demo plugin.
Version: 1.0
Author: Gurjot Singh
Author URI: https://bhattigurjot.wordpress.com
License: GPL2
*/
?>

We’ll save the file. You can write anything you want. But this information is very essential if we want our plugin to display in the Plugins section in our WordPress.

Now we can see our plugin listed among all other plugins. Yey! 🙂

Okay, moving on to the next part, we’ll create a simple HTML text in another PHP file.

We’ll now open a new document and write the following code:

<?php
function myfn() {
$html=' Hello! This is my first Plugin ';
}
?>

We’ll save this file as ‘function.php’ in our same demo directory.
We now have this function which contains html text but how do we display this text in our page?
To do this we must include this function.php file in our main ‘demo.php’ plugin file by adding the following code:

include ("function.php");

Even if we’ve included our ‘myfn()’ function in our demo.php we can’t display it as such. And we don’t even know where to display the text.

In order to do that we create a shortcode that we insert in any page or post to display our plugin’s content. To create our own shortcode we write the following in our demo.php file:

if ( !is_admin() )
add_shortcode('mydemo', 'myfn');

This creates a shortcode [mydemo] for our function myfn() that displays the text.
Now the complete demo.php file looks as follows:

<?php
/* Plugin Name: demo
Plugin URI: http://demo.wordpress.com
Description: A demo plugin.
Version: 1.0
Author: Gurjot Singh
Author URI: https://bhattigurjot.wordpress.com
License: GPL2
*/
include ("function.php");
if ( !is_admin() )
add_shortcode('mydemo', 'myfn');

?>

Ok so its time to activate our plugin.
After activating it we can insert our shortcode ‘[mydemo]’ in any page or post we want.

And now we’ll see our ‘Hello! This is my first Plugin’ text in that page or post.

That’s it Folks! We have successfully created our very own first plugin. CHEERS! 😀
If you are having some trouble then don’t hesitate to ask.
And do read WordPress codex for plugin development.
Writing a Plugin
Plugin API
Plugin Resources
These are very important.
Post your comments below and I’ll see you (text-ly) next time. 😛 Till then Keep Developing!

Advertisements

26 thoughts on “Creating Our First WordPress Plugin

  1. Hey there! I know this is somewhat off topic
    but I was wondering which blog platform are you using for this
    site? I’m getting fed up of WordPress because I’ve
    had issues with hackers and I’m looking at options for another platform. I would be great if you could point me in the direction of a good platform.

    1. Well actually I am using wordpress. But if you’d really want some other platforms you can read this article. Its brief and descriptive and really Dan Mckee did a great comparison for all the platforms. I think it may really help you choose your blog platform. 🙂

  2. Excellent pieces. Keep writing such kind of information on your blog.
    Im really impressed by your blog.
    Hey there, You have performed a great job. I will definitely digg it and
    individually recommend to my friends. I’m confident they will be benefited from this site.

  3. I blog quite often and I genuinely thank you for your information. This article has truly peaked my interest.

    I will book mark your website and keep checking for new information about once per week.
    I opted in for your RSS feed too.

  4. Hi there! I could have sworn I’ve been to this website before but after browsing through some of the post I realized it’s new to me.
    Anyhow, I’m definitely happy I found it and I’ll be book-marking and checking back often!

  5. Appreciating the hard work you put into your blog and in depth information you offer.
    It’s good to come across a blog every once in a while that isn’t the same old rehashed material.
    Fantastic read! I’ve bookmarked your site and I’m including your RSS feeds to my Google account.

  6. Its like you learn my thoughts! You appear to understand a lot approximately this, such as you wrote
    the ebook in it or something. I feel that you can do with some percent to power the message home
    a little bit, however other than that, this is wonderful blog.
    A fantastic read. I will definitely be back.

  7. Hey There. I found your weblog the use of msn. That is a really smartly written article.

    I will make sure to bookmark it and come back to learn more of
    your helpful information. Thank you for the post. I’ll
    definitely comeback.

  8. I truly love your website.. Great colors & theme.
    Did you develop this amazing site yourself? Please reply
    back as I’m hoping to create my own personal blog and
    would love to find out where you got this from or what the
    theme is called. Kudos!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s