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.
Create a directory with your Plugin name.(Like : myPlugin/
)
In the Plugin directory (myPlugin/
) create a PHP file with the same name of the Plugin directory(Like : myPlugin.php
).
myPlugin.php
myPlugin.php
File:In this file you need to write some codes and functions.
/**
* 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
*/
$wpdb
.
global $wpdb;
include "functions.php";
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');
add_action()
function.
function myplugin_menu(){
}
myplugin_menu()
function: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');
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');
If you want to create table at the database for your plugin, then follow the below steps.
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);
}
register_activation_hook()
function and pass the previously declared function into register_activation_hook()
.
register_activation_hook(__FILE__,'instal_tables');
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.php
myplugin_menu_page_view.php
myplugin_menu_page_view.php
file contain menu or 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
}
?>
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";
Plugin creation has been completed. Whole plugin folders and files structure is given below.
myPlugin.php
myplugin_menu_page_view.php
myplugin_submenu_page_view.php
Now with two ways you can set up your Plugin at the WordPress.
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.
Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request
Excellent
I am beginner of wordpress and first time i want to learn how to create in wordpress plugin. please you can share different different page of given above code
Thanks for sharing, this is a fantastic article post. Fantastic.
Very good article post.Really looking forward to read more. Really Cool.