preloader

Today I will explain to you how to create your own WordPress plugin. It is part of the basics in WordPress Development such as create your own child theme.

Why is it useful to create his own plugin?

If you cannot help customizing any pieces of elements on your website such as changing the background color of the nav main menu or adding some new texts or buttons before or after some content you will understand why it is so useful to create his own plugin.

Why should I create my own plugin?

When you want to customize your website, you are told to write some pieces of code in your functions.php file you find in your theme folder “wp-content/themes/<your-theme-name-folder>”. The problem is when your theme is updated or if for any reason you want to change the theme you can lose or deactivate all your changes.

That is why using a plugin gives you more flexibility and security to modify your website layout and/or functioning. All your changes will be saved in a different folder and will be themes of plugins update-proof.

Moreover, with a plugin, you will gain in code organization and clarity (I assure you will be happy to divide your 1000 code lines of your functions.php into 2 or 3 folders and 10 .php files. Code corrections or updates will be quicker!).

But first of all, if you want to create your own Worpress customizations and share them with the others, a plugin is the easiest way :).

Let’s not beat about the bush!

The first step to create a plugin is to create a folder in “wp-content/plugins/” named <your-plugin-name>. Of course, your folder name shall not contain any space ;). You can see in this folder all the other installed plugins on your WordPress website!

The second step is to create, this time, a file named <your-plugin-name.php> in which you have to copy-paste the following piece of code. The file shall have exactly the same name as the folder you created before. Otherwise, the WordPress engine will not detect your plugin.

<?php
/*
Plugin Name: <your-plugin-name>
Plugin URI: <your-website-url>
Description: <plugin-description>
Version: <version>
Author: <your-name>
Author URI: <your-website-url>
Text Domain: <your-plugin-name>
Domain Path: /languages/
License: <your-plugin-license>
*/

It is important that the code is wrapped in php comment tag. I am sure you wonder what means all these fields. I will explain them to you :

  • Plugin Name: The name of your plugin as it will appear in your admin panel > Plugins.
  • Plugin URI: The link you see under your plugin description.
  • Description: The plugin description. Be concise but clear. It is what your customer will see for the plugin description.
  • Version: As you can guess, it is the version of your plugin.
  • Author: You name or nickname.
  • Author URI: Your personal creator website. If you write multiple plugins, it is where you can find them all.
  • Text Domain: This field is very important if you want your plugin to be multilingual. It is the string you will write every time you use the following functions “__(‘Some text’, ‘your-plugin-name’)” or “__(‘Some other text’, ‘your-plugin-name’)”.
  • Domain Path: Where all your language files will be saved.
  • License: As you can guess again, the license of your plugin!

You just created your first plugin, congratulations! Now you have to activate it in the Admin Panel > Plugins. You can write here all the customization code you want without fear it will be deleted after any WordPress or Theme updates.

To go further and create a complete plugin with admin panel interactions

Great, you know the basics to create a custom plugin, but how to add quick access in the admin panel to collect users’ inputs? It is what I will show you!

Quick access in the admin panel

In your plugin .php file, you created, you will need to copy-paste the following piece of code

add_action('admin_menu', 'my_plugin_setup_menu');
 
function my_plugin_setup_menu(){
        add_menu_page( 'My Plugin Page', 'My Plugin', 'manage_options', 'my-plugin', 'my_plugin_admin_content' );
}
 
function my_plugin_admin_content(){
        echo "<h1>Hello World!</h1>";
}

The first line “add_action([…])” is a normal WordPress action_hook. If you are not familiar with WordPress hook, I recommend you to read the WordPress developer codex for the explanation. Briefly, it means that when the WordPress engine will load the admin function codes, the “my_plugin_setup_menu” function will be called.

In “my_plugin_setup_menu” we see the “add_menu_page” function. This function allows for creating quick access.

  1. The first attribute is the name of our options page.
  2. The second attribute is the name that will be displayed in your admin plugin page.
  3. ‘manage-options’ give the access level of our page. It is internally defined by the WordPress engine with Roles & Capabilities.
  4. The fourth attribute is the identifier slug of your menu
  5. The last is the name of the function that will be called when the page is viewed.

Here is the result!

You just created your first plugin with admin quick access. You can now communicate with users to collect inputs and therefore make your plugin customizable.

Here is a list of useful functions to know in WordPress plugin development :

And of course the WordPress Function Codex.

Leave a Reply

Your email address will not be published. Required fields are marked *