Develop WordPress Plugin From Scratch – Part 1

If you are reading this article then you are already familiar with WordPress which is most used CMS in world. Ever wondered why it is most popular CMS, of course it is easy to set up and use. Second reason it has huge repository of free plugins and themes. Plugins add more additional functionality to your WordPress website. Ahh, you already know that that’s why you want to code your own first WordPress plugin. This guide will teach you from scratch and at the end of this tutorial series you would be able to easily code your WordPress plugin.


                                                       Download Plugin


1)      Basic WordPress knowledge. I assume you have already installed it in your local machine.

2)      Basic knowledge of PHP, SQL would be a plus.

What we are going to develop?

We are going to develop a useless plugin (from users perspective) 😛 What? yeah, it is just for developers who want to learn plugin development by doing. But after this series of plugin development tutorial I am sure you would be able to create a good and useful plugin with that idea in your mind. This simple plugin would teach you following tasks:

  • Create blank WordPress plugin.
  • Create and drop tables on activating and deactivating plugin.
  • Create admin menu for your plugin.
  • Display data from database in admin section.
  • Delete data rows from admin section.

In first part we will cover only these things but we have a huge list to cover in future posts in this series.

Let’s start our first part of ‘developing WordPress plugin from scratch’:

1. Create Plugin for WordPress:

WordPress stores it’s all plugin files in wp-content/plugins folder. So you also need to create a folder ‘myplugin’ inside  wp-content/plugins. Create a file infotuts.php in ‘myplugin’ folder now add some information about your plugin in this file.

NOTE: You can create folder and file with any name. WP reads all files and finds main file with following information about the plugin.

Plugin Name: InfoTuts WP Plugin Tutorial
Plugin URI:
Description: Test plugin to teach plugin development to new developers
Author: InfoTuts
Version: 0.1
Author URI:

Now this is your blank plugin and it won’t do anything but still you can navigate to plugins page in your WP dashboard and see it listed there.

2. Create and Drop tables Dynamically:

Lets create some constants which will be used later in our code:

$getsiteurl = get_option('siteurl'); // fetches your website url from wp_options table
define('INFOT_FOLDER', dirname(plugin_basename(__FILE__))); // __FILE__ will give path of //infotuts.php
define('INFOT_URL', $getsiteurl.'/wp-content/plugins/' . INFOT_FOLDER);

Now we will write code to create tables when our plugin will be activated and those tables will be dropped upon plugin deactivation. So we need to have a way to activate and deactivate our plugin, well luckily you don’t need to worry about that. WordPress already offers you functions for both activation and deactivation of plugin.

Lets have a look at our code line by line. Here we are using register_activation_hook() funtion which will be called once we activate our plugin.

global $wpdb; // wordpress global database object
// this is the table prefix
$infotuts_table_prefix = $wpdb->prefix.'infotuts_';
define('INFOT_TABLE_PREFIX', $infotuts_table_prefix);
// function to activate plugin
// function to deactivate plugin
register_deactivation_hook(__FILE__ , 'infot_plugin_uninstall' );

Below I will explain about activation and deactivation function.

register_activation_hook( $file, $function );
$file - (string) (required) — Path to the primary plugin fi le
$function - (string) (required) — The function to be executed when the plugin is activated

and register_deactivation_hook() function has similar definition which goes like this:

php register_ deactivation_hook( $file, $function );
$file - (string) (required) — Path to the primary plugin fi le
$function - (string) (required) — The function to be executed when the plugin is deactivated

Now we have simple function infot_plugin_install written to create table in our MySQL database and another function infot_plugin_uninstall to drop that table.

function infot_plugin_install()
 global $wpdb;
 $table = INFOT_TABLE_PREFIX."demotable";
 $schema = "CREATE TABLE $table (
 name VARCHAR(255) NOT NULL,
 email VARCHAR(255) NOT NULL,
 website VARCHAR(255) NOT NULL,
 description text,
 UNIQUE KEY id (id)
 // Populate table
 $wpdb->query("INSERT INTO $table(name, email, website, description)
 VALUES('Sanjeev','','','Programming blog')");
function infot_plugin_uninstall()
 global $wpdb;
 $table = INFOT_TABLE_PREFIX."demotable";
 $deletetable = "drop table if exists $table";

3. Create Admin Menu for Plugin:

WordPress makes it pretty simple to create menu in admin panel for your plugin. WordPress offers add_menu_page and add_submenu_page functions for the same. We will use add_action function to hook infotuts_master_menu on specific action which is admin_menu.

Below is the code for the same:

add_action('admin_menu','infotuts_master_menu'); // 'infotuts_master_menu' would be called
function infotuts_master_menu() {
if (function_exists('add_menu_page')) {
 /** add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );
 * add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function);
 * $page_title=write on page title
 * $menu_title= Menu name on display Dashboard
 * $capability= user Level Capability in access point top capability is 10 and low is 0.
 * $menu_slug=The slug name to refer to this menu by (should be unique for this menu)
 * $function= new function creat on dashboard.
 * $icon_url=icon url
 * $position=position menu on dashboard.
 * 2 Dashboard,4 Separator, 5 Posts, 10 Media, 15 Links, 20 Pages, 25 Comments, 59 Separator, 60 Appearance, 65 Plugins, 70 Users,
 * 75 Tools, 80 Settings, 99 Separator
 "InfoTuts Plugin tutorial",
 "InfoTuts Plugin tutorial",
 add_submenu_page(__FILE__,'Listing plugin data','plugin data','8','list-plugin-data','infotuts_admin_list_data');
function infotuts_admin_menu_lists()
 echo "Creating WP Plugin is fun!!!!!";
// function to display data
function infotuts_admin_list_data()
 include 'infotuts-admin-data.php';

In above code we have included ‘infotuts-admin-data.php‘. This file will display data in a table and we have also written code to delete rows.

4. Delete rows from admin panel:

we have created ‘infotuts-admin-data.php‘ to display data in a table and delete rows. Below is its code:

 // Columns heading
$columns = array(
 'name' => 'Name',
 'email' => 'Email',
 'website' => 'Website',
 'description' => 'Description',
 'action' => 'Action'
 register_column_headers('infotuts_data', $columns);
<div class="wrap">
 <?php echo "<h2>" . __( 'Sample Data' ) . "</h2>"; ?>
<table class="widefat page fixed" cellspacing="0">
<?php print_column_headers('infotuts_data'); ?>


// Code to delete rows
global $wpdb;

$wpdb->query($wpdb->prepare("DELETE FROM $table WHERE id = %d", $_GET['delete']));
// display table's data
$admin_url = admin_url('admin.php?page=list-plugin-data&', 'http');
$sql = "SELECT * FROM ".INFOT_TABLE_PREFIX."demotable where 1";
$results = $wpdb->get_results($sql);
if(count($results) > 0)
 foreach($results as $result)
 echo "<tr>
 <td>".$result-> name."</td><td>".$result->email."</td><td>".$result->website."</td>

 <td><a href='".$admin_url."delete=".$result->id."' >Delete</a></td>

If you have followed this tutorial then I am sure you can now perform basic operations using plugin. We will extend functionality of this plugin in our next post. We will also learn how to style your plugin and some cool tricks to help you in your plugin development career. I would be happy to help any query you have got during this simple tutorial.

About sanjeev

Dreamer, Blogger and Thinker. I love to help people in solving their problems. You can also join me HERE

Related posts:

3 Responses so far.

  1. Hi Sanjeev,

    nice tutorial!
    Can you teach how can you display that table on the page? so the user can view the records also.


Comments are closed.