{"id":1968,"date":"2021-01-26T12:00:00","date_gmt":"2021-01-26T11:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/how-to-build-a-wordpress-plugin-from-the-scratch-part-1\/"},"modified":"2026-07-05T03:25:18","modified_gmt":"2026-07-05T01:25:18","slug":"how-to-build-a-wordpress-plugin-from-the-scratch-part-1","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/how-to-build-a-wordpress-plugin-from-the-scratch-part-1\/","title":{"rendered":"How to Build a WordPress Plugin From the Scratch \u2013 Part 1"},"content":{"rendered":"<p>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.<\/p>\n<p>As you know,\u00a0 plugins are added to WordPress installation. So, we need need access to a WordPress installation files.<\/p>\n<p>Here are the thing we would cover in this part:<\/p>\n<ol>\n<li><a href=\"#t1\">Enable Debugging<\/a><\/li>\n<li><a href=\"#t2\">Setup Your Plugin<\/a><\/li>\n<li><a href=\"#t3\">Activation and Deactivation<\/a><\/li>\n<li><a href=\"#t4\">Perform Some Action<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Enable Debugging<\/strong><\/h4>\n<p>Open your the root folder of your site and find the file named: <em>wp-config-sample.php.<\/em> Open the file and set the WP_DEBUG to true. To to this, just change from to true as in the line below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">define( 'WP_DEBUG', true );\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Setup your plugin<\/strong><\/h4>\n<p><strong>Step 1<\/strong> &#8211; 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 <em>the-genius-plugin<\/em>.<\/p>\n<p><strong>Step 2<\/strong> &#8211; Inside this folder, create a php file with same name (<em>the-genius-plugin.php<\/em>)<\/p>\n<p><strong>Step 3<\/strong> &#8211; Add the following as content of the file:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #557799;\">&lt;?php<\/span>\r\n<span style=\"color: #dd4422;\">\/**<\/span>\r\n<span style=\"color: #dd4422;\"> * @package TheGeniusPlugin<\/span>\r\n<span style=\"color: #dd4422;\"> *<\/span>\r\n<span style=\"color: #dd4422;\"> * \/<\/span>\r\n \r\n<span style=\"color: #dd4422;\">\/*<\/span>\r\n<span style=\"color: #dd4422;\">Plugin Name: The Genius Plugin<\/span>\r\n<span style=\"color: #dd4422;\">Plugin URI: http:\/\/kindsonthegenius.com<\/span>\r\n<span style=\"color: #dd4422;\">Description: This is you first plugin from Kindson The Genius<\/span>\r\n<span style=\"color: #dd4422;\">Version: 1.0.0<\/span>\r\n<span style=\"color: #dd4422;\">Author: Kindson Munonye \"Kindson The Genius\"<\/span>\r\n<span style=\"color: #dd4422;\">Author URI: http:\/\/kindsonthegenius.com<\/span>\r\n<span style=\"color: #dd4422;\">License: GPLv2 or later<\/span>\r\n<span style=\"color: #dd4422;\">Text Domain: the-genius-plugin<\/span>\r\n<span style=\"color: #dd4422;\"> *\/<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 4<\/strong> &#8211; Now go to your WordPress dashboard. You&#8217;ll see the plugin we created as shown below:<\/p>\n<figure id=\"attachment_14561\" aria-describedby=\"caption-attachment-14561\" style=\"width: 1024px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/01\/Screenshot-2021-01-26-at-15.16.21.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-14561 size-large\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/01\/Screenshot-2021-01-26-at-15.16.21-1024x214.png\" alt=\"Your first WordPress Plugin\" width=\"1024\" height=\"214\" srcset=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/01\/Screenshot-2021-01-26-at-15.16.21-1024x214.png 1024w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/01\/Screenshot-2021-01-26-at-15.16.21-300x63.png 300w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/01\/Screenshot-2021-01-26-at-15.16.21-768x161.png 768w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/01\/Screenshot-2021-01-26-at-15.16.21-600x125.png 600w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/01\/Screenshot-2021-01-26-at-15.16.21.png 1038w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption id=\"caption-attachment-14561\" class=\"wp-caption-text\">Your first WordPress Plugin<\/figcaption><\/figure>\n<p>Isn&#8217;t this amazing!<\/p>\n<p>Note that, you can activate the plugin. However, noting happens when you activate and deactivate since we&#8217;ve not written any code.<\/p>\n<p><strong>Step 5<\/strong> &#8211; Finally, in the same folder as the plugin, create a index.php file. The content would just be an opening &lt;?php tag.<\/p>\n<p><strong>Step 6<\/strong> &#8211; Add the following code to your the-genius-plugin.php file:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">defined('ABSPATH') or die('You can access this location!');\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Activation and Deactivation<\/strong><\/h4>\n<p>We now need to write what will happen when the user activates, deactivates or uninstall the plugin. So follow the steps below.<\/p>\n<p><strong>Step 1<\/strong>: Create a new class in the plugin file. The content will be:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"> class TheGeniusPlugin {\r\n     \r\n     function activate() {\r\n         echo 'Plugin was activate';\r\n     }\r\n     \r\n     function deactivate() {\r\n         echo 'Plugin was deactivated';\r\n     }\r\n     \r\n }\r\n \r\n if(class_exists('TheGeniusPlugin')){\r\n     $theGeniusPlugin = new TheGeniusPlugin();\r\n }\r\n \r\n \/\/ activation\r\n register_activation_hook(__FILE__, array($theGeniusPlugin, 'activate'));\r\n \r\n  \/\/ deactivation\r\n register_deactivation_hook(__FILE__, array($theGeniusPlugin, 'deactivate'));\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 2:<\/strong> Save the file and then go to activate the plugin. You&#8217;ll notice an error message. So let&#8217;s handle this<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Perform Some Action<\/strong><\/h4>\n<p>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:<\/p>\n<p><strong>Step 1<\/strong>: Write the following function inside the class we created:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"> \/\/Create a student custom post type\r\n function custom_post_type() {\r\n     register_post_type('student', ['public' =&gt; true, 'label' =&gt; 'Students']);\r\n }\r\n<\/pre>\n<p>This function will create a new Students item in the dashboard.<\/p>\n<p><strong>Step 2<\/strong>: You then need to call this function in the TheGeniusPlugin construction. So add the constructor to the class:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"> function __construct() {\r\n     add_action('init', array($this, 'custom_post_type'));\r\n }\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>You can now test the plugin by activating and then refreshing the page. You will see the Students tab as shown below:<\/p>\n<figure id=\"attachment_14562\" aria-describedby=\"caption-attachment-14562\" style=\"width: 300px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/01\/Screenshot-2021-01-26-at-16.26.02.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-14562\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/01\/Screenshot-2021-01-26-at-16.26.02-300x248.png\" alt=\"Custom Post Type plugin\" width=\"300\" height=\"248\" srcset=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/01\/Screenshot-2021-01-26-at-16.26.02-300x248.png 300w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/01\/Screenshot-2021-01-26-at-16.26.02.png 322w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-14562\" class=\"wp-caption-text\">Custom Post Type plugin<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p><strong>Step 3<\/strong>: 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.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">flush_rewrite_rules();\r\n<\/pre>\n<p>You can also add it to the deactivate() function.<\/p>\n<p><strong>Optionally<\/strong>: Call the custom_post_type explicitly in the activate function. Use the code below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">flush_rewrite_rules();\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[414],"tags":[],"class_list":["post-1968","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1968","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=1968"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1968\/revisions"}],"predecessor-version":[{"id":2136,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1968\/revisions\/2136"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1968"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}