A Simple Guide on How to Create a Custom WordPress Plugin from Scratch

Read below to understand how to create a custom WordPress plugin from scratch! : WordPress is one of the most popular open-source content management that allows people to create web applications. It has many plugins that provide easy modification, customization, enhancement of a WordPress blog, and more.

On the other hand, a WordPress plugin is a program or a bundle of more functions. But written in PHP is an excellent source to add a specific set of features or services to WordPress without changing the core programming of WordPress.

Do you look for a custom WordPress plugin from scratch? If yes, in this tutorial we have put together the information on how to build a WordPress plugin from scratch. WordPress Developers can easily understand how to build a custom WordPress plugin from scratch through this tutorial. Check out here the step-by-step tutorial to create a custom WordPress Plugin.

Step by Step guide on how to create a Custom WordPress Plugin

Here are the steps for creating a custom WordPress plugin from scratch.

Step 1st – Start Creating a Directory

To create a custom WordPress Plugin, create a directory with your plugin name.

For example – myPlugin/

Step 2nd – Create a PHP file for Main Plugin File

After creating a directory with your plugin name, the second step is to create a PHP file to get a main plugin file with the same name as the Plugin directory in the Plugin directory (myPlugin/).

For example –

myPlugin.php

Step 3rd – Write some codes and functions in myPlugin.php File

In this step, open myPlugin.php File and write some codes and functions.

  • This file includes a standard Plugin information header. Below is information that should be given in 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

a. Whether your file shows or has any function that interacts with the database, declare the $wpdb.

Like – global $wpdb;

b. Whether your file shows or has any function that interacts with the database, declare the $wpdb.

Like – global $wpdb;

c. After this, create a functions.php file to create some user-defined functions and include them.

Like – include “functions.php”;

d. Then, you will declare the add_action() function: add_action() function Hooks a function onto a specific action.

//add_action( $tag, $function_to_add, $priority, $accepted_args );
add_action('admin_menu', 'myplugin_menu');

e. Besides this, you have to declare the function that you already added into the add_action() function.

function myplugin_menu(){
}

Step 4th – Create myplugin_menu() function

After working in the myPlugin.php file, declare myPlugin_menu() Function. To run properly myPlugin_menu() Function, you have to keep in your mind these points.

a. declare the add_menu_page() function.

For example –

//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');

b. declare the add_submenu_page() function.

For example –

//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');

Step 5th – Creation of Database Table

The fifth step is the creation of a Database Table. To create a table in the database for your plugin you may be followed the below-given steps.

a. declare a user-defined function like

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);
}

b. declare register_activation_hook() function.

Like –

register_activation_hook(__FILE__,'instal_tables');

Step 6th – Consider Page View

In this step, you have to create a directory under the Plugin directory. After this, you have to create a PHP file for page view. Read the below instructions to understand more.

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

Then, declare the subdomain 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
}
?>

Step 7th – Now, Include Page View

You have to include all files which include the menu and submenu page functions. Like –

include "pages/myplugin_menu_page_view.php";
include "pages/myplugin_submenu_page_view.php";

Step 8th – Congratulation!

Thus, your plugin has been completed to run. All plugin folders and files structure here.

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

Step 9th – Installation

Installation is the final step of creating a custom WordPress plugin from scratch. Here are some ways to set up your plugin in WordPress.

The first way is to copy the whole plugin folder and paste it into the WordPress Plugins directory (wp-content/plugins).

The second way is to make a zip of your plugin folder and upload your Plugin zip from the wp-admin panel. It may include some steps – like

– log in 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.

Thus, we have explained to you how to create the custom WordPress Plugin creation tutorials..

Leave a Comment