[Ultimate Guide] How to create wordpress plugin and upload to wordpress.org | Creating wordpress plugin | Part - 1


What is WordPress?

At its core, WordPress is the simplest, most popular way to create your own website or blog. In simple terms, it is a CMS (Content Management System) which is licensed under GPLv2, which means that anyone can use or modify WordPress for free. A content management system is basically a tool that makes it easy to manage important aspects of your live site - like media, content - without needing to know anything about programming. 

What is this article about? 

At the core of WordPress, it has several built-in features for basic management but additional features can be installed in it for advanced management which is called Plugins. Here, Plugins in WordPress mean extensive features to give the user ease of using WordPress.

Who should read this post? 

One who wants to contribute to the WordPress plugin community or making one personal plugin for his ease of managing his specific website with prior PHP knowledge. 

This will be a series of 10 parts and will be further continued if requested. Before beginning below are some of the requirements to get started:

  • A prior PHP, HTML, CSS knowledge.
  • A code editor recommended VS CODE.
  • WordPress installed in your local system

So let's execute it. 

Project Setup:

In this part, we will learn to create a basic plugin i.e only the user interface to show up in the admin dashboard of WordPress. Hence the folder structure will not be Professional. Hoping that you have pre-installed WordPress let's continue the steps

STEP 1 - Create a Folder with the specific name of the plugin in the WordPress 'wp-content/plugins' folder located in the root directory of  WordPress.

STEP 2 - Create an index.php folder which will the main file which will import functions from other files and also will contain the plugin details like its name, version, author, e.t.c. So we will be mainly editing this file keep checking the caption below the codes to see which file is being edited. 

/** * Plugin Name: Your Plugin Name * Plugin URI: https://Yourppluginuri.com * Description: Plugin Description * Version: 1.0.0 * Requires at least: 5.2 * Requires PHP: 7.2 * Author: Your Name * Author URI: Your AUthor Url * License: License Type * License URI: License Url * Text Domain: my-basics-plugin * Domain Path: /languages */
index.php

Add this to the index.php file just created and don't forget to edit the values. You could easily understand the above code so I am not going to explain the above code let go ahead and open your WordPress plugins section. If you have done everything correctly you will be able to see a new deactivated plugin in the WordPress plugins section with its details now let's move ahead by creating menus and sub-menus for the plugin.

STEP 3 - Before continuing to add menus we would add activation and deactivation function so that after the plugin is either activated or deactivated it will run some code like create a table in the WordPress database.

register_activation_hook( __FILE__, 'activation_function_name' ); register_deactivation_hook( __FILE__, 'deactivation_function_name' ); function activation_function_name(){ global $table_prefix, $wpdb; $tblname = 'your_table_name'; $wp_table = $table_prefix . "$tblname "; if($wpdb->get_var( "show tables like '$wp_table'" ) != $wp_table) { $sql = "CREATE TABLE `". $wp_table . "` ( "; $sql .= " `id` int(11) NOT NULL auto_increment, "; $sql .= " `pincode` int(128) NOT NULL, "; $sql .= " PRIMARY KEY `order_id` (`id`) "; $sql .= ") ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; "; require_once( ABSPATH . '/wp-admin/includes/upgrade.php' ); dbDelta($sql); } } function deactivation_function_name(){ global $wpdb; $table_name = "your_table_name'; $sql = "DROP TABLE IF EXISTS $table_name;"; $wpdb->query($sql); }
index.php
Now, let's understand the code. First, in register_activation_hook(), it will register a hook which will be called during its activation, and __File__ constant returns the full path and name of the PHP file that's being executed and the second value is the function name which would be executed. The same applies to the register_deactivation_hook(). Now we have declared both the hooks and their specific functions and let's understand the activation function code:
In the code, $wpdb is the global variable of database connectivity and $table_prefix is the prefix of the database tables given by the user which is 'wp_' for default. Then it checks if the table already exists if it exists the function skips the step and moves ahead else creates a new table.
And deactivation function is just its opposite which checks if the table exists or not, if it exists it deletes the table otherwise moves ahead.

STEP 4 - Adding menu to the dashboard:

add_menu_page(string $page_title, $menu_title,string $capability, string $menu_slug, callable $function)

Above is the code structure which we will use first let's understand it. The function add_menu_page will add a hook to make a menu item in which the $page_title will be the title of the menu item page and $menu_title will be the text to be shown in menu item next is a capability which would tell WordPress that this menu will do in our case we would use 'manage_options' as the capability. Next, menu_slug will be the URL slug through which one can access the menu page and the slug must not contain whitespace and the last one is the function to be called on the page-load of the menu item. This is the top-level menu which would contain submenus if you want.

Also you need to wrap these code in a function below is a full code of the menu and submenu

function mysite_admin_menu(){ add_menu_page('Members', 'Members', 'manage_options', 'members-slug', 'members_function'); add_submenu_page( 'members-slug', 'Add Members', 'Add Members', 'manage_options', 'add-members-slug', 'add_members_function'); } add_action('admin_menu', 'mysite_admin_menu');

Here is some details about the parameters

‘Members’ = Page Title

‘Members’ = Menu Title

‘manage-options’ = Capabilities required for this menu

‘members-slug’ = Menu slug shows on the url

‘members_function’ = Output of this function will show on the menu page.

In add_submenu_page() function we see everything is same except ‘members-slug’ on the first parameter. This slug refers the dropdown menu under the main menu.

After adding all these codes we would have index.php look like this:
 function activation_function_name(){ if (get_var( "show tables like '$wp_table'" ) != $wp_table) { $sql = "CREATE TABLE `". $wp_table . "` ( "; $sql .= " `id` int(11) NOT NULL auto_increment, "; $sql .= " `pincode` int(128) NOT NULL, "; $sql .= " PRIMARY KEY `order_id` (`id`) "; $sql .= ") ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; "; require_once( ABSPATH . '/wp-admin/includes/upgrade.php' ); dbDelta($sql); } } function deactivation_function_name(){ global $wpdb; $table_name = "your_table_name'; $sql = "DROP TABLE IF EXISTS $table_name;"; $wpdb->query($sql); } function mysite_admin_menu(){ add_menu_page('Members', 'Members', 'manage_options', 'members-slug', 'members_function'); add_submenu_page( 'members-slug', 'Add Members', 'Add Members', 'manage_options', 'add-members-slug', 'add_members_function'); } add_action('admin_menu', 'mysite_admin_menu'); ?> /** * Plugin Name: Your Plugin Name * Plugin URI: https://Yourppluginuri.com * Description: Plugin Description * Version: 1.0.0 * Requires at least: 5.2 * Requires PHP: 7.2 * Author: Your Name * Author URI: Your AUthor Url * License: License Type * License URI: License Url * Text Domain: my-basics-plugin * Domain Path: /languages */

This was it for part-1, in part-2 we would learn how to add a settings page to the plugin till then stay tune ad check our site daily.

Here will be some plugin ideas you could implement to improve your personal blog.
I don't know if these plugins exist but if you want to practice or make one for your personal use you can go on with these.

  • All in one plugin: It will have tons of features put into a single plugin.
  • Cache Plugin: Your own cache plugin to improve your site speed.
  • Amp plugin: For creating mobile-accelerated pages.
  • Other plugins that would use APIs to perform specific functions.
Thanks for reading the next part would be published tomorrow and the last i.e part 10 would contain a detailed guide on setting up the project structure to publish it to wordpress.org and make it publically available. If you liked my content or have any issues comment below.