+92 308 5039935
Get a Free Quote
+92 308 5039935

How to create WordPress plugin

How to create WordPress plugin

Plugins are used to extend WordPress functionality or add a new feature. There are more than 54K+ plugins available free at WordPress Plugins directory and tons are available at other markets places. So chances are very high that you will find the functionality you need. Still there comes a time when you need to build a custom plugin. Keep in mind this is not a beginner tutorial you must know PHP well to understand this tutorial.  This is how I build plugins for my self and my clients, other developers or you may differ.

Our plugin name will be Xe Plugin so lets create folder name xe-plugins with sub-folders assets, inc and languages. Create css and js sub-folders inside assets. Now lets create the files we need, readme.txt, xe-plugin.php and uninstall.php. Then create main.css in css folder and main.js in js folder.

Add the following text to the xe-plugin.php file. Remember to change Plugin Name, Description, Version, Author, Author URI and Text Domain accordingly.

Now lets create a class and name it Xe_Plugin with __construct() method inside it. Make sure to check if the class isn’t already existed and then instantiate it after creating. This will be the main class and will contain all the functions, classes etc.

Inside __construct() add the following code. I will explain it later on.

Its time to add methods/functions for register_activation_hook()register_deactivation_hook() and load_plugin_textdomain() inside the class.

As for initialize() that will be the main method/function which will have all the functionality.  Just add the function for initialize() right after __construct() or any where you like.

readme.txt can be generated on GenerateWP. All my plugin’s readme.txt file are simple as following.

Now you may think what is the uninstall.php for? Some clean-up is needed when the plugin is uninstalled from a site. For example removing plugin options or settings specific to the plugin and database entities such as tables. You can read more about it on the Plugin Handbook.

The plugin is created. If you put this in plugins directory it will show up and you will be able to activate it. But remember it does nothing as there is no functionality added. You can add anything you need or want to add to your WordPress site. The possibilities are beyond imagination and this tutorial. I will cover adding some functionality using this plugin we created here in the next blog posts.

Click Here to download the plugin we have created from github.

 

Hello, I am a web designer and developer working since 2013. I build WordPress themes, write and extend WordPress plugins, edit WordPress themes and plugins in accordance to customer needs and play games when free ;-)
Related Post
Minify CSS
How to minify CSS in WordPress Theme or Plugin

Minification is used to remove unnecessary elements, e.g: extra spaces. There are a lot of php scripts to minify or compress the css but we are going to use a simple function  here. It is usually used for inline css in WordPress.

Read more
How to add inline CSS in WPBakery Page Builder Element

WPBakery Page Builder (Formerly Visual Composer) is one of the best drag and drop front-end/back-end builder. You can easily apply inline css using style attribute but what if you need to apply css to a pseudo elements?

Read more
How to Auto Load Files in WordPress?

You may need to auto load files in WordPress themes or plugins. But I wouldn’t recommend it if you are creating theme to sell on themeforest. They didn’t accepted themes with this auto load function few years ago when we used

Read more

Leave a Reply

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