How to create custom plugin in WordPress

Custom plugin is used to customize the wordpress default functionalities both admin and frontend.

WordPress provides some standard which need to follow to create plugins.

The steps are as follows.

1. Create a directory for custom plugin under plugins directory. Create a php file under the new directory and put the following code in it.

At first you need to define plugin header as follows.

<?php
/*

Plugin Name:Custom Plugin

Description:This is a custom plugin for learning.

Author:Gaurav Srivastav

Version:1.0

*/

?>

After defining header the plugin will be listed in admin panel under plugins menu. You can activate/deactivate/delete the plugin from here.

2. Create tables on table activation

register_activation_hook( __FILE__, array( 'CustomPlugin', 'install' ) );
global $wpdb;
$employee = $wpdb->prefix . "Employee";
if($wpdb->get_var("show tables like '$employee'") != $employee) {
$sql = "CREATE TABLE " . $employee . " (
id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
salary int
PRIMARY KEY(id)
);
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}

3. Define shortcode for plugin for frontend.

add_shortcode('shortcode_name','function_to_call');
//exmaple below
add_shorcode('booking_engine','booking_template');

function booking_template(){

include_once "booking-form.php";

}

Leave a Reply