In this tutorial, you will learn how to build a WordPress plugin from the scratch! This would be a practical tutorial with all the code snippet you need available. Also watch the video for more explanation.
As you know, plugins are added to WordPress installation. So, we need need access to a WordPress installation files.
Here are the thing we would cover in this part:
1. Enable Debugging
Open your the root folder of your site and find the file named: wp-config-sample.php. Open the file and set the WP_DEBUG to true. To to this, just change from to true as in the line below:
define( 'WP_DEBUG', true );
2. Setup your plugin
Step 1 – Once you have chosen a name for your plugin, then create a folder with same name inside the plugins folder. For me, I call it the-genius-plugin.
Step 2 – Inside this folder, create a php file with same name (the-genius-plugin.php)
Step 3 – Add the following as content of the file:
<?php /** * @package TheGeniusPlugin * * / /* Plugin Name: The Genius Plugin Plugin URI: http://kindsonthegenius.com Description: This is you first plugin from Kindson The Genius Version: 1.0.0 Author: Kindson Munonye "Kindson The Genius" Author URI: http://kindsonthegenius.com License: GPLv2 or later Text Domain: the-genius-plugin */
Step 4 – Now go to your WordPress dashboard. You’ll see the plugin we created as shown below:

Isn’t this amazing!
Note that, you can activate the plugin. However, noting happens when you activate and deactivate since we’ve not written any code.
Step 5 – Finally, in the same folder as the plugin, create a index.php file. The content would just be an opening <?php tag.
Step 6 – Add the following code to your the-genius-plugin.php file:
defined('ABSPATH') or die('You can access this location!');
3. Activation and Deactivation
We now need to write what will happen when the user activates, deactivates or uninstall the plugin. So follow the steps below.
Step 1: Create a new class in the plugin file. The content will be:
class TheGeniusPlugin { function activate() { echo 'Plugin was activate'; } function deactivate() { echo 'Plugin was deactivated'; } } if(class_exists('TheGeniusPlugin')){ $theGeniusPlugin = new TheGeniusPlugin(); } // activation register_activation_hook(__FILE__, array($theGeniusPlugin, 'activate')); // deactivation register_deactivation_hook(__FILE__, array($theGeniusPlugin, 'deactivate'));
Step 2: Save the file and then go to activate the plugin. You’ll notice an error message. So let’s handle this
4. Perform Some Action
What we want is to have custom post types when this plugin is activated. The name of the custom post type will be Students. Follow the steps:
Step 1: Write the following function inside the class we created:
//Create a student custom post type function custom_post_type() { register_post_type('student', ['public' => true, 'label' => 'Students']); }
This function will create a new Students item in the dashboard.
Step 2: You then need to call this function in the TheGeniusPlugin construction. So add the constructor to the class:
function __construct() { add_action('init', array($this, 'custom_post_type')); }
You can now test the plugin by activating and then refreshing the page. You will see the Students tab as shown below:

Step 3: Finally, you need to flush the rewrite rules. This means that data would refresh automatically without you having to refresh the page. So put the code below in the activate() function.
flush_rewrite_rules();
You can also add it to the deactivate() function.
Optionally: Call the custom_post_type explicitly in the activate function. Use the code below:
flush_rewrite_rules();