+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
Introduction to Websites & WordPress

School of Entrepreneurship and XeCreators collaboratively arranged a one-day workshop on “Introduction to Websites and WordPress” specially for people with no IT background.

Read more
How to Increase the Maximum File Upload Size in WordPress

There comes a time when you need to upload a larger size file but the default file upload size limit is low as 2mb. You can increase WordPress file upload size limit either by your self or contact your hosting provider to do it for you.

Read more
How to Create Shortcode in WordPress

Shortcodes are very helpful in WordPress for adding custom code inside posts or pages. Although you can use drag-n-drop builders (e.g: WPBakery Page Builder, Elementor etc.)

Read more

Leave a Reply

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