Create WordPress Plugin from scratch

 

Open source content management system WordPress is the most popular web application in the world today. Plugins for WordPress make it easy to modify, customize, and enhance blogs. WordPress Plugins are programs, or sets of functions, written in the PHP, that add a specific set of features to WordPress without changing the core programming of the system.

 

We will show you how to build a custom WordPress plugin from scratch in this tutorial. This will be useful for WordPress developers. It is very easy to develop a custom WordPress plugin with the following step by step tutorial. We will show you how to develop WordPress plugins easily. Start creating WordPress plugins now.

1) Plugin Directory:

Create a directory with your Plugin name.(Like : 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).

3) myPlugin.php File:

Codes and functions need to be written in this file. The file must contain a standard Plugin information header. Comments should contain the information.  

/**
 * 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 );
add_action('admin_menu', 'myplugin_menu'); 

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()
{
    global $wpdb;
    require_once(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";
    dbDelta($table1); 

Now declare the register_activation_hook() function and pass the previously declared function into register_activation_hook().register_activation_hook(__FILE__,’instal_tables’); 

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.

<?php
    ob_start();
    //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>
    <?php
    }
?> 

7) Include view pages

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

include "pages/myplugin_menu_page_view.php";
include "pages/myplugin_submenu_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.

 

*

Post a Comment (0)
Previous Post Next Post