How To Create a WordPress Plugin? Beginner’s guide.

How To Create a WordPress Plugin

Written by - adeena

August 4, 2020

Here, you’ll learn, how to create a WordPress plugin along with its admin page. Interesting right? Let’s start.

Why create a WordPress Plugin? The most important reason is, it allows you to separate your code from the WordPress core code which is why the rest of your website will continue functioning smoothly if there is an issue in your plugin.

Things You’ll Need To Create a WordPress Plugin

  1.  WordPress installation.
  2.  Text or Code Editor.
  3.  Hosting account FTP access.
  4.  Basic knowledge of PHP.

Note: Before we create a WordPress Plugin, it’s highly recommended to create a backup of your website if you have any previous data in it.

What Is WordPress Plugin?

A WordPress plugin, a standalone code set that extends the functionality of WordPress. By using any combination of PHP, HTML, CSS, JavaScript/jQuery, or the other web programing language, a plugin can add extra new features to any part of your website, obviously including the Admin Control Panel.

Even you can customize default behavior or completely remove unwanted functionality. It allows you to simply customize and personalize WordPress according to your needs.

What are Hooks to create a WordPress Plugin?

Let’s get a little more technical. Plugins literally ‘plugin’ to WordPress core which is done using ‘hooks,’ that enable one piece of code to interact with another. As such, hooks determine when and where on your site a plugin is used.

Confused? if you’re new to the topic, let’s consider an example. Imagine you have a plugin that changes the error message that appears when somebody tries to sign in to your site with the wrong password.

In this scenario, the error message is the hook. A plugin could connect to the code that displays that message and change the text that’s shown.

Following are the different types of hooks, using it, WordPress Plugins interacts with core code.

  1. Action hooks – add/remove functionality
  2. Filter hooks – modify the functionality of actions

There’s a lot more to how hooks work here in Plugin Developer Handbook.

Now that you have a basic understanding of hooks and filters, let’s create a simple WordPress plugin that adds a new link and page to the Admin Control Panel. To create a WordPress plugin you need to follow some steps.

Step 1 – Storing Your Plugin

To create a WordPress plugin, make a folder to store all your files. Plugins are saved in the following folder: /wp-content/plugins/.

Note: You need to create a unique and descriptive name to ensure it doesn’t clash with any other plugin.

Connect to your hosting account with an FTP client. Navigate to wp-content from the main directory, then go to plugins. Inside the plugins folder, create a new folder named my-first-plugin.

 

How to create a plugin

 

To make things a lot easier for yourself, you must separate the various files of your WordPress plugin into their subfolders, instead of having them in the main folder.

When a plugin grows and becomes complex, it will never create a mess to find a specific file when everything has its place. If your plugin has custom CSS or JavaScript, you create their relevant folders and save all files in there respectively.

Step 2 – Creating the First File

The first file in your plugin is an important one. It contains all the information WordPress needs to display your plugin in the plugin list, which allows you to activate the plugin.

In your my-first-plugin folder, create a new PHP file named my-first-plugin.php. It’s good practice to give this first file a similar name to the one you gave your folder, but it can have any name you like.

Add the opening PHP tag <?PHP to the first line. You don’t need to add a closing tag to the end of the file (to understand why to read the note on this page of the PHP manual).

This file will primarily hold ‘header comments’ with various pieces of information that will be read/displayed by WordPress. Header comments go in a multi-line PHP comment, one per line and each line starts with a specific piece of text to define what the line refers to.

These should only go in this first file and do not need to be in any other file. Write like this;

<?php
/*
Plugin Name: My First Plugin
Description: This is my first plugin! It makes a new admin menu link!
Author: Your Name
*/

 

Save and upload the file to a my-first-plugin folder. Navigate to the Plugins to view a plugin named My First Plugin.

 

 

How to create a plugin

Step 3 – Writing Your Plugin’s Functions

Talking about best practices when developing a plugin you should neatly separate your code into appropriate files and folders or categories.

Since the primary job of the first file is to hold the comment headers, it makes sense and a lot easier to place the rest of the plugin’s code in separate files in their subfolder, using PHP’s ‘include’ functions to access them.

Any files stored in subfolders are called directly by our code and only by our code, so subfolder names don’t need a prefix.

However, it is highly recommended that you give all your files, functions, and variables a unique prefix in their name to avoid any conflicts with other plugins. In this case, we are using MFP as a prefix, which is short for ‘My First Plugin’.

Creating a new folder:

In the plugin’s main folder, create a new folder named includes. Any file that is ‘included’ by another file will go in this it.

Let’s create a new PHP file in the includes folder and save it as MFP-functions.php. Start it with <?PHP tag. This new file will store all your plugin’s functions.

Now go back to my-first-plugin.php in the plugin’s main folder. Include the MFP-functions.php file to use the new functions in any other file in your plugin.

Use require_once to ensure the plugin only works if the functions file is available. The easiest way to include is by using the WordPress function plugin_dir_path(__FILE__).

It gives the full path to the directory where our plugin is stored, then uses a. (period) to append the name of the subfolder we created earlier (includes), followed by the name of the file we created (MFP-functions.php).

Edit my-first-plugin.php as shown below then save and upload it once again, overwriting the previous version when asked.

<?php
/*
Plugin Name: My First Plugin
Description: This is my first plugin! It makes a new admin menu link!
Author: Your Name
*/// Include mfp-functions.php, use require_once to stop the script if mfp-functions.php is not found
require_once plugin_dir_path(__FILE__) . 'includes/mfp-functions.php';

 

In MFP-functions.php, write the following:

 

 

<?php
/*
 * Add my new menu to the Admin Control Panel
 */// Add a new top level menu link to the ACP
function mfp_Add_My_Admin_Link()
{
  // My code goes here
}

 

Inside our function, we need to use the built-in WordPress function add_menu_page() to give our menu a name,  a title, and dictate who is allowed to see it.

 

 

Required Parameters:

The required parameters of add_menu_page() appear on their line to improve readability, in this order:

  1. The title of the page (see after clicking the link displayed in your browser)
  2. Text to show as the menu link (displayed in the admin control panel navigation list), it should be the name of the plugin
  3. User capability requirement to view the menu, in this example only users with the ‘manage_options’  can access the page.
  4. The file to use when displaying the actual page which will be stored in the includes subfolder and named MFP-first-ACP-page.php and the URL known as a ‘slug’.

Before continuing, it’s crucial to note that there is another way to use this function. The fourth parameter can simply be a string of text which is displayed in the URL after ‘wp-admin/admin.php?page=’.

If you enter ‘my-plugin-page’, URL becomes ‘wp-admin/admin.php?page=my-plugin-page’. The fifth parameter must then be the name of a function that outputs anything.

You could write a function that just echoes ‘Hello World’ for instance. It is significantly easier to create a PHP file to hold your page.

Edit MFP-functions.php, replace the comment with add_menu_page(), and give it parameters as shown below:

<?php
/*
 * Add my new menu to the Admin Control Panel
 */// Add a new top level menu link to the ACP
function mfp_Add_My_Admin_Link()
{
      add_menu_page(
        'My First Page', // Title of the page
        'My First Plugin', // Text to show on the menu link
        'manage_options', // Capability requirement to see the link
        'includes/mfp-first-acp-page.php' // The 'slug' - file to display when clicking the link
    );
}

 

To make this function run, we need to use the WordPress function named add_action() with two parameters, first is the action hook you want to target. i.e. admin_menu.

 

 

In our case and the second parameter is just the name of the function to run.

The function we wrote is named mfp_Add_My_Admin_Link. Upload MFP-functions.php to the includes folder and overwrite the old one. Given below is the Final Look.

<?php
/*
 * Add my new menu to the Admin Control Panel
 */// Hook the 'admin_menu' action hook, run the function named 'mfp_Add_My_Admin_Link()'
add_action( 'admin_menu', 'mfp_Add_My_Admin_Link' );// Add a new top level menu link to the ACP
function mfp_Add_My_Admin_Link()
{
      add_menu_page(
        'My First Page', // Title of the page
        'My First Plugin', // Text to show on the menu link
        'manage_options', // Capability requirement to see the link
        'includes/mfp-first-acp-page.php' // The 'slug' - file to display when clicking the link
    );
}

 

Step 4 – Create An Admin Page

This page will be displayed when you click on your admin control panel link. Go to the includes subfolder and create a new PHP file named MFP-first-ACP-page.php.  Write the HTML given below, and upload the file.

Hello!This is my plugin's first page

Go back to your plugin list in the WordPress Admin Control Panel and activate the plugin. There is a plugin named, ‘My First Plugin’. Click it, and you have your very own admin control panel page.

 

 

Hurrah! you have just created your first simple WordPress plugin. Now, you can play a bit to build upon what you’ve learned and extended WordPress any way you like.

Final look How to create a WordPress plugin

This blog is published for beginners to create their first WordPress Plugin. Hopefully, you will find it very informative to create your first plugin. Glowlogix (Pvt) Ltd has created its front-end plugin. You can download it. It’s free to use on your website.

You May Also Like…

No Results Found

The page you requested could not be found. Try refining your search, or use the navigation above to locate the post.