• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Featured
    • C# Tutorials
      • LinQ Tutorials
      • Facebook C# API Tutorials
    • PHP Tutorials
      • CodeIgniter Tutorials
    • Amazon AWS Tutorials
  • Categories
    • Programming
    • Development
    • Database
    • Web Server
    • Source Control
    • Management
    • Project
  • About
  • Write
  • Contact

CodeSamplez.com

Programming, Web development, Cloud Technologies

You are here: Home / Development / How To Create A Simple Wordpress Plugin

How To Create A Simple WordPress Plugin

November 13, 2010 by Rana Ahsan 9 Comments

Wordpress Plugin Development

WordPress is a very popular development platform for the PHP developers in blog site/simple CMS development.  As it became the most popular blogging platform, demand for WordPress plugin development on this platform also increased rationally. There is already resources on WordPress developers center on how to write plugin , but you may find it a little bit descriptive where you really interested in a quick start. Here , i will describe how to start writing a very simple plugin very fast. Our goal from this tutorial is to first make a plugin that will say ‘hello world’ at the beginning of every WordPress posts, then we will enhance this plugin to generate and ‘Facebook Like’ button that will be shown up at the beginning of each post. Of course, I will provide code samples as well to create this plugin on each step for sure. I am assuming here that you have a little knowledge about how WordPress works and familiar with the folder structure.

Where To Place The Plugin And How?

You will see a folder named ‘plugins’ in your ” {root_directory}/wp-contents ” directory. You will have to place your source code on that folder. You can either place it in a single file or use multiple files in a folder. in case of using folder, use the main PHP file name should be same as the folder name. Also, while choosing folder/main file name, please take an unique name so that your plugin doesn’t collide with any other plugin. Lets assume we are using multiple file, so we are gonna create a folder name ‘like-fb’ and then create a file named ‘like-fb.php’ .

What To Do To Appear The Plugin In The Admin Panel’s Plugin Section?

WordPress has some pre-defined rules with which it pares the developed plugin files and retrieve information from them. To get a plugin appear in the admin panel’s plugin section,  simply add few lines in the PHP file just like below, of course you will like to place your own plugin name/URL etc instead of mine:

/*
Plugin Name: Like FB
Plugin URI: http://codesamplez.com
Description: A very basic test plugin
Version: 1.0
Author: Md. Ali Ahsan Rana
Author URI: http://i2dev.com
License: GPL2
*/

Here every points are self descriptive, so I am not going to tell about these one by one. You can place these info with any order, WordPress will be able to read it, no problem.

Our Hello World Function

The following function will print a hello world message when it’s called. First we will prepare the function, then will make some arrangement to call it according to when we want.

function say_hello($post_ID)  {
echo 'Hello world. This is my first wordpress plugin';
return $post_ID;
}

How To Tell WordPress To Call This Function?

We must have to tell WordPress in some way, when to call the above function.  To facilitate this, wordpress provides us a set of code execution points when it will  execute our functions. These points are called hooks. We won’t be able to execute any of our plugin functionality besides those hook points, but the existing ones are good enough for our regular needs. For this plugin, we need to call our function before showing a post content. So, we are going to use like this:

add_action ( 'the_content', 'say_hello');

Here, ‘the_content’ is the hook point we are setting to execute our functions. There are lot more, see available hook lists.

Now if you just go to your admin panel’s plugin section, you will see your plugin with un-activated status. Now, please active the plugin. And we are done. Now checkout your site. And what? our message is showing just before the post content on every post, its cool, isn’t it?

Turning This To Facebook Like Button Plugin:

Ok, just saying a message isn’t enough yet. We want to do some solid functionality, though simple. ok, Lets make our function modified a little as follows:

function fb_like($post_ID)  {
?>
<iframe src="http://www.facebook.com/plugins/like.php?href=<?php echo urlencode('http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]); ?>&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe>
<?php
    return $post_ID;
}

As we see, this function actually prints a Facebook like button code. Here, URL is always the current page URL, so that it reflects the current page URL for every post. Now change the hook settings code to the correct function name(here ‘fb_like’):

add_action ( 'the_content', 'fb_like');

We are done. Checkout your site, we now have a Facebook like button on every post page. 😀

You can also download this Like FB Plugin For WordPress to compare if you are having trouble with your one.

Here, by the above php code examples, I have just tried to give you a quick but interesting start. You can do a lot more during wordpress plugin development. Such as, by giving users to set different settings on admin panel, triggers the plugins on many more hook points, etc. Like, to extend the current plugin, you can first try to set some admin options just like you can see on the Facebook like button build page. Then change like button code according to user preference, save their preferences etc, and you will get more interactivity on this plugin too. Happy coding 🙂

Share If Liked

  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on Pocket (Opens in new window)

You may also like

Filed Under: Development Tagged With: php, plugin, wordpress

About Rana Ahsan

Rana is a passionate software engineer/Technology Enthusiast.
Github: ranacseruet

Reader Interactions

Comments

  1. engin says

    April 14, 2013 at 9:03 am

    Thanks. it’s useful tutorial

    Reply
  2. Damien Foord says

    May 11, 2013 at 6:24 pm

    Would this have to be a plugin per se? Could you not just add the function to your theme functions . php file?

    Reply
    • Md Ali Ahsan Rana says

      May 23, 2013 at 12:47 pm

      sure, we could. As I mentioned, this is simple plugin just for demonstration purpose for the tutorial. Real time plugins are with lot more complex functionality.

      Reply
      • rabindra chauhan says

        November 28, 2014 at 12:34 pm

        hi sir, could you share a video regarding wordpress and wordpress plugin in hindi. please.

        Reply
  3. dsislo says

    May 28, 2013 at 12:51 pm

    I’m new to WP. I want to have my users sign in to one of my pages via their email address. Can I do this on my “parent” page and if so, what is the easiest way. Any help is so appreciated.

    Thanks –

    Reply
  4. Fl'âme says

    December 28, 2013 at 12:17 pm

    Thanks! Starting coding =) …

    Reply
  5. shailendra mahanande says

    July 30, 2014 at 5:22 am

    At the time of plugin active i got this message so how can I over come it ?

    The plugin generated 1 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.

    Reply
  6. nevaza says

    July 13, 2015 at 1:13 pm

    Really useful and simple plugin tutorials, good job Ali… thank you for sharing.

    Reply
  7. thangavel .R says

    August 22, 2016 at 7:28 am

    great job..

    Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,774 other subscribers

Follow Us

  • Twitter
  • Facebook

Top Posts & Pages

  • How To Work With JSON In Node.js / JavaScript
    How To Work With JSON In Node.js / JavaScript
  • PHP HTML5 Video Streaming Tutorial
    PHP HTML5 Video Streaming Tutorial
  • How To Work With C# Serial Port Communication
    How To Work With C# Serial Port Communication
  • Facebook C# API Tutorials
    Facebook C# API Tutorials
  • LinQ Query With Like Operator
    LinQ Query With Like Operator
  • Using Supervisord Web Interface And Plugin
    Using Supervisord Web Interface And Plugin
  • Get Facebook C# Api Access Token
    Get Facebook C# Api Access Token
  • Generate HTTP Requests using c#
    Generate HTTP Requests using c#
  • Getting Started With UDP Programming in Java
    Getting Started With UDP Programming in Java
  • Utilizing Config File In C#.NET Application
    Utilizing Config File In C#.NET Application

Recent Posts

  • Building Auth With JWT – Part 2
  • Building Auth With JWT – Part 1
  • Document Your REST API Like A Pro
  • Understanding Golang Error Handling
  • Web Application Case Studies You Must Read

Tags

.net angularjs apache api audio auth authenticatin aws c# cloud server codeigniter deployment docker doctrine facebook git github golang htaccess html5 http javascript jwt linq mysql nodejs oop performance php phpmyadmin plugin process python regular expression scalability server smarty socket.io tfs tips unit-test utility web application wordpress wpf

Footer

Archives

Follow Us

  • Twitter
  • Facebook

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,774 other subscribers

Copyright © 2023