WordPress Plugin Development: Getting Started

Welcome to the WordPress Plugin Development Tutorial. This blog post will help you to start how to develop WordPress Plugins. If you already have an experience in WordPress Plugin Development, you can review and suggest changes to the post.

WordPress plugins are one of the major components of WordPress. The plugins help to extend the website features or add additional features to the WordPress website. The basic idea behind plugins is to not edit any core files of WordPress but add additional features by extending the core functionality.

WordPress Plugin

WordPress plugins are the external packages that depend on the WordPress core and extend it to add additional features or override some features. WordPress plugins are created using PHP so you must have prior experience in PHP before we can start developing WordPress plugins.

What can you expect from this WordPress Plugin Development Series?

  • You will learn how WordPress Plugins are Developed.
  • You will also learn some best practices on WordPress Plugin Development.
  • You will create a Quotes Of The Day Plugin.

I assume that you already have a web server ( xampp ) installed on your machine. I am using Manjaro distro and if you are a Linux user, you might need to provide additional permission to your webserver directory.

If you are not using Linux, skip this part.

You can give permission of 755 which means, Readable by everyone, writeable by the Author and executable by everyone. Here everyone refers to WordPress that needs to read and execute some files when we are working on our project.

Open your terminal and change the directory to your web server folder ( lampp ).

Get root access, you can directly run the command as root too.


sudo chmod -R 755 /lampp/htdocs/

Now, all the directories and files inside htdocs has 755 permission.


Creating our first “Hello, World” Plugin

Our first plugin will be very simple, few lines of code and we will output a demo text just to add some value to the very first post about WordPress Plugin Development. After all, no one likes theory, isn’t it?

When you are done with installing your web server, you need to create a project folder and name it anything you like. I will name it hello-world.

Now, download the latest version of WordPress. Install it on the project folder. In my case, I will install the WordPress core on my hello-world folder.

Let me explain about the folder structure of WordPress, if you check the core structure, you will find wp-content the folder where all the stuff about themes, plugins, and media are located. Inside it, you will find plugins folder where all the plugins you installed on your website is located. Same goes with themes folder.

So, we are creating a plugin, we need to create our plugin directory inside plugins folder. Create a folder with anything you like to name, I will go with my root name which is hello-world.

Let’s create our first PHP file for the plugin. While developing a plugin, your plugin name must be unique so that it will not conflict with other plugins installed on the plugins directory. Also, another important thing is to use the same plugin name in your PHP file so that WordPress will use it to recognize it as a plugin core file.

In my case, I have a plugin name called hello-world so my file name will be same hello-world.php.

Let’s set up our plugin names and all the details that will be listed under the WordPress Dashboard > Plugins list.

Open your plugin file hello-world.php and start to write PHP comments.

<?php

/**
 *
 * @package hello-world
 *
 * Plugin Name: Hello World
 * Plugin URI: https://www.ashiish.me/
 * Description: A plugin to greet the visitors or just another test plugin to be honest.
 * Version: 1.0.0
 * Author: Ashish Yadav
 * Author URI: https://www.ashiish.me/
 * License: GPLv2 or later
 * Text Domain: hello-world
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * 
 */

Don’t worry about closing the php. WordPress will handle it. Also, now if you go to your dashboard > plugins, you will find your plugin is available for activation with all the details we have listed above like name, version, description.

Hello World Plugin

Currently, our plugin does nothing because we have not instructed it to do. There are many things need to be taken care of before we instruct.

First thing first, our plugin is not secure and can create a security issue to a WordPress website if you use it. Anyone can make direct access to the plugin file and attempt the malicious activity.

I just wanted to help you all get started and add some value to the very first post about WordPress Plugin Development, we will take care of security and all other important stuff. Let’s just skip those stuff for now and make our plugin do something.

You can activate the plugins from the plugins list. Once it is activated we will output some text using our plugin somewhere on our website.

<?php

/**
 *
 * @package hello-world
 *
 * Plugin Name: Hello World
 * Plugin URI: https://www.ashiish.me/
 * Description: A plugin to greet the visitors or just another test plugin to be honest.
 * Version: 1.0.0
 * Author: Ashish Yadav
 * Author URI: https://www.ashiish.me/
 * License: GPLv2 or later
 * Text Domain: hello-world
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * 
 */

// Dummy thank you function
function thank_you() {
	echo "Hello, this is my first plugin and thank you, Ashish, for this tutorial.";
}
// call the function
thank_you();

Save your plugin file and check the website. You will find the text output somewhere at the top of the website.

Text output by the plugin on top of the header.

Currently, we have lots of issues with our plugin that I have listed above but the one more mistake is, our text will be visible everywhere on the website including the WordPress Dashboard because every time you refresh the page the plugin is called which outputs the text.

We can easily prevent this behavior using the simple built-in method provided by WordPress.

You can check if the page is not admin page using is_admin() method.

function thank_you() {
  // check if the page is not an admin page
  if(!is_admin()) {
    echo "Hello World, this is my first plugin and thank you, Ashish, for this tutorial.";
  }	
}

thank_you();

Here you go, a simple plugin that outputs a string in front end when activated.

This is just an example of WordPress Plugin Development on our first part. We will create a secure and nice plugin from next blog post. Thank you for reading. Feel free to comment down below and share if you love this post.

Check another part of this series:
WordPress Plugin Development: Basics OOP Concepts