Create a custom WordPress Plugin from scratch

WordPress is the most popular open source content management system for today’s world web application. WordPress Plugins allow easy modification, customization, and enhancement of a WordPress blog. A WordPress Plugin is a program, or a set of one or more functions, written in the PHP, that adds a specific set of features or services to the WordPress without changing the core programming of WordPress.

In this tutorial we will describe the creation of a custom WordPress Plugin from the scratch. WordPress Developers can learn how to create custom WordPress Plugin. The following step by step tutorial has made the custom WordPress plugin development process very simple. You can easily create WordPress plugin with our tutorial. Let’s start the WordPress Plugin creation tutorial.

1) Plugin Directory Creation:

Create a directory with your Plugin name.(Like : myPlugin/)

  • myPlugin/

2) Main Plugin File:

In the Plugin directory (myPlugin/) create a PHP file with the same name of the Plugin directory(Like : myPlugin.php).

  • myPlugin/
    • myPlugin.php

3) myPlugin.php File:

In this file you need to write some codes and functions.

  • This file must contain a standard Plugin information header. The information should be given into the comment tags.
     * Plugin Name: Name Of The Plugin
     * Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
     * Description: A brief description of the Plugin.
     * Version: The Plugin's Version Number, e.g.: 1.0
     * Author: Name Of The Plugin Author
     * Author URI: http://URI_Of_The_Plugin_Author
     * License: A "Slug" license name e.g. GPL2
  • If your Plugin have any function that interact with the database, then declare the $wpdb.
    global $wpdb;
  • If you want to create some user defined functions, then create functions.php file and include it.
    include "functions.php"; 
  • Declare the add_action() function : add_action() function Hooks a function on to a specific action.
    //add_action( $tag, $function_to_add, $priority, $accepted_args );
  • Now declare the function which you previously added into add_action() function.
    function myplugin_menu(){

4) myplugin_menu() function:

  • Declare the add_menu_page() function : add_menu_page() function creates a new top level menu section in the admin menu sidebar and registers a hook to callback your function for outputting the page content when the linked menu page is requested.
    //add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );
    add_menu_page(' My Plugin'' My Plugin ''administrator''myplugin_settings''myplugin_ menu_page'); 
  • Declare the add_submenu_page() function : add_submenu_page() function add a sub menu page.
    //add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function );
    add_submenu_page('myplugin_settings''My Plugin Submenu''My Plugin Submenu''administrator''myproject_submenu''myproject_submenu_page'); 

5) Database Table Creation :

If you want to create table at the database for your plugin, then follow the below steps.

  • At first declared a user defined function like install_tables().Then you should be declared global $wpdb; and include upgrade.php file for database connection. After that assign a variable with the value of the create table SQL and pass this variable into the dbDelta() function.
    function instal_tables()
    ABSPATH 'wp-admin/includes/upgrade.php');
    $table1 "CREATE TABLE ".$wpdb->prefix."table_name (
             `id` bigint(100) NOT NULL AUTO_INCREMENT,
             `name` varchar(255) NOT NULL,
             `email` varchar(255) NOT NULL,
             PRIMARY KEY  (`id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci"
  • Now declare the register_activation_hook() function and pass the previously declared function into register_activation_hook().

6) Page view :

Now create a directory under the Plugin directory. Then create a PHP file for page view and insert this file into the newly created directory. For better understanding please follow the below file structure.

  • myPlugin/
    • myPlugin.php
    • pages/
      • myplugin_menu_page_view.php

  • The myplugin_menu_page_view.php file contain menu or submenu page function.
  • This PHP file contain menu or submenus function. If you want to use this page for menu page view, then you need to declare the menu page function. If you want to use this page for submenu page view, then you need to declare the submenu page function.
    //admin menu page function
    function myplugin_menu_page()
    //for db connection
    global $wpdb;
            <!-- Page HTML goes here -->
            <h1>This is my Plugin menu page</h1>

7) Include view pages

Include all files which are contains the menu and submenu page functions.

include "pages/myplugin_menu_page_view.php";

8) Congratulation!

Plugin creation has been completed. Whole plugin folders and files structure is given below.

  • myPlugin/
    • myPlugin.php
    • pages/
      • myplugin_menu_page_view.php
      • myplugin_submenu_page_view.php

9) Installation :

Now with two ways you can set up your Plugin at the WordPress.

  • First, you can copy the whole plugin folder and paste it into the WordPress Plugins directory (wp-content/plugins).
  • Second, you can make a zip of your plugin folder and upload your Plugin zip from the wp admin panel.(login to the wp admin panel => click on the Plugins link from the left menu section => click the Add New => upload your Plugin => active your plugin)

 We have been completed the custom WordPress Plugin creation tutorials. If you have any query about this tutorial and scripts, feel free to comment here.

Related Articles

Comments 0