How to make a basic WordPress Plugin

If you are a website developer, you might come across a client or situation where you need to create your own plugin, so here’s a quick tutorial on how to do this. First you need a code editor, we use Visual Studio Code.

Then you need to create the basic files. At first, we can just use 2 files, index.php and the name of your plugin, let’s say “my-first-plugin.php”.

We have the index.php file as a security measure so people can’t access the plugin code. Within this folder, add the code:

<?php

die('you cannot be here');

?>
This basically gives the would-be hacker a message saying you cannot be here, this is the first layer of defence.
Now if you go to the website WordPress developer handbook, you’ll see lots of usful info but for now all we need is this page https://developer.wordpress.org/plugins/plugin-basics/header-requirements/ this gives us the header requirements to get started. You’ll see you need at least the following code to get started:
/*
* Plugin Name: YOUR PLUGIN NAME
*/

You may also need to put <?php at the top of the file to make sure your code edit knows you are coding in PHP. Then you have the basics of a WordPress plugins and you can add this to your WordPress site, unfortunalty it does not thing at this point. We should next add another layer of protection.

We should now add the following code under

if( !defined('ABSPATH') )

{

die('You cannot be here');

}
This code checks if you user has got to this code via the abolsule path, which is the path that WordPress uses to access it, so if anyone tries to access these files via the browser they will be given the “die” message. 

Creating some simple functionality in your WordPress plugin

In order for our plugin to actually do something we need to add some more code, as an example we can hide the admin bar on the frontend. You may need to do this for a number of reasons. You can find this code online, we found it here https://themeisle.com/blog/code-snippets-for-wordpress/#gref. Just copy and paste the code under the absolute path code and save it. Then when you add your new plugin it will have some real useful functionality.

// Remove the admin bar from the front end

add_filter( 'show_admin_bar', '__return_false' );
To add this code to your WordPress site, simply ZIP up the folders and add it as you would add any other plugin, when you then go to the front-end, it should hide the admin bar.