Create WordPress Plugin : WordPress Essentials

Create WordPress Plugin

So once you go through this post you will come to know how to create wordpress plugin. This post will have a series of other posts as well which will let you know how to create a widget in the wordpress plugin, how to work with the database through the plugin, how to work on wordpress admin section through a plugin and how to create shortcodes in a wordpress plugin . So Once you go through all the future extensions of this post you will come to know all about the wordpress plugins.

Declaring a WordPress Plugin

Plugins have a very basic structure similar to as themes. So to declare a plugin in wordpress there are very small number of action that must be executed, and then its up to you to add functions and complete their structure .

All the plugins are located in the “wp-content/plugins” folder. Plugin can consist of multiple files. So the name of our plugin will be “Scratch Plugin”

First, let’s create a “scratch” folder so that our path would be “wp-content/plugins/scratch” in which we will add a file called  scratch.php.

So for WordPress to recognize the plugin , it is necessary for us to declare at least its name in the main file. All this declaration is done via a comment at the beginning of the file.

If you want to give more information about your wordpress plugin, especially when you want to distribute the plugin, the following information is recommended after the name comment.

  • Plugin URI: the website address of  your plugin describing all of its operation and giving more additional information
  • Description: a small description of the plugin, will displayed when browsing the list of plugins
  • Version: the version of the plugin
  • Author: your name
  • Author URI: the address of your author’s site
  • License: A license of your plugin.

So now our scratch.php will look like this

Wordpress has now recognized your plugin! To check it out, go to the WordPress Admin. You will see it appearing in lists of installed plugins. Plugins -> Installed Plugins . If you see it, activate it (see the following figure)!

Create WordPress Plugin

Using an object structure while you create a wordpress plugin

As the power of php lies in classes therefor we will structure our plugin using classes, But this is not mandatory as a plugin can work simply using the functions only . But in our case we will be using Object Oriented approach.

So in our scratch.php file we will create a  Scratch_Plugin class with a wordpress filter add_filter(). So our scratch.php file will look something like this.

Working of above code

Firstly the class gets initialized via “new Scratch_Plugin();” object then the construct function is called. construct function calls the wordpress hook function “add_filter”  which then calls our “change_page_title” function. Now when you will go to any post page . The post title will be changed. change_page_title plugin then returns the title which would be shown.

To Read More About add_filter You Can Go To The Following Link : https://developer.wordpress.org/reference/functions/add_filter/

A multi-file structure while you create wordpress plugin

When you start to create your wordpress plugin, you would probably need to write the code in multiple files. So lets elevate our create wordpress plugin tutorial by creating mutli-file structured. In a wordpress plugin only the main file is automatically included in the application, all the other files must be included by us if we want to use them.
So our scratch.php will also load the files we need to load in the wordpress plugin. To illustrate this, we will create a new file post-title.php with class Scratch_Post_Title which will contain our add_filter code.
So our scratch.php file will look like this.

Create a new file in our plugin directory(wp-content/plugins/scratch/) named post-title.php which will have the following code.

Working of above code

So the functionality of all the above code will be same as we created in a single file . So what is happening here. In scratch.php the object is initialized “new Scratch_Plugin();” which calls the constructor. Within the constructor we included or file/class post-title.php and initialize the class with its object “new Scratch_Post_Title();” which will call its own constructor and then “add_filter” hook will change the title of our posts in wordpress.

Create a complete wordpress plugin

Now its time to go a step further while we create wordpress plugin. Now we will take all the possibilities which wordpress has to offer us. Now we will move into more advance topics while creating a wordpress plugin as we move on further. The main goal will be to create a Newsletter plugin.

There are multiple things which we would be doing for this plugin.

  • Create A Newsletter Widget.
  • Displaying Newsletter on all the pages.
  • Database for storing all the emails from the newsletter
  • Administration interface for showing the content of email

So for this our scratch.php file will look like this.

We will create a new file “scratch-newsletter.php” with class name Scratch_Newsletter this class will have all the code related to our newsletter functionality. For now we will add a sample code in our scratch-newsletter.php