WordPress is undoubtedly the most powerful CMS framework available in the recent times. From the creation of highly flexible and extendable websites with very little work; we believe the Plugins and Themes structure is the main reason behind its success as a CMS.
Considering that there are around 21,000 plugins available for free on the official WordPress website, mastering the skills of plugin development is highly recommended for every WordPress Plugin Developer. Not only will it help you create your own plugins whenever possible, but also help you customize your site seamlessly and earn good money as a WordPress developer. Here we discuss a set of must-know skills for WordPress plugin development:
#1. Creating a Plugin
The step towards WordPress Plugin Development is to create your own plugin folder in the /wp-content/pluginsfolder. Once created, make sure that you place all your plugin files inside that folder. Also ensure that files are named in simple letters with hyphens (-) for separating words. For example, wp-multi-slider.php. Therein, insideyour main file; you should have a comment structure in order for WordPress to identify your plugin. This will enable you to see the plugin in the Dashboard Plugins section.
#2. Plugin Activation/Deactivation
This is a simple process where you are required to activate Plugins by just clicking Activate Link in the plugin list. While, you do not require doing anything on activation of a simple plugin, an advanced plugin requires tasks like initializing plugin options, creating plugin tables, etc. So, this calls for mastering the challenge as to how we can handle plugin activation and deactivation.
#3. Plugin Activation Hook
All familiar with WordPress knows that it provides a function called register_activation_hook that initiates with plugin activation. Also, we can add a custom function to execute on plugin activation using this method.
Now, the catch comes – if you pass the path of the file that contains the activation function as the first parameter and the function name as the second parameter. If the activation function is inside the main plugin file you can use _FILE_. This allows you to do tasks like validations, initializations and table creations inside the activation function.
#4. Plugin Deactivation Hook
Just using similar syntax as with activation hook, you can handle the ‘deactivation’ of a plugin with register_deactivation_hook. This will help you clean up the plugin resources, options and tables inside the deactivation function.
#5. Creating Custom Tables
Even though you have a fairly flexible WordPress database table structure, where you can implement most of the custom functionalities using the available tables; there remains the possibility of situations where you wish to include more advanced systems like shopping carts, task management systems, or booking systems.
Such cases require you to know how and when to create custom tables. Here what you need to do is first consider the requirements of your projects and try to use wp_options table and meta tables to store your project specific data. And in case you feel that the tables aren’t structured enough to implement the required functionality, you can create custom tables. For doing so, you will need to check out a few things:
Before setting out to create a table, first check if the table already exists. Thereafter, you can move ahead and decide to drop and create the table on activation based on your requirements. A point to remember while creating custom tables is that generally, WordPress tables are prefixed with wp…This can actually be changed when you install the database; so you should not hardcode wp_ as a prefix to provide increased flexibility.
#6. Including Scripts and Styles
Even though WordPress allows you to you can just echo the scripts and styles anywhere, it is highly recommended that you add scripts using the wp_enqueue_script function. The main purpose of this function is to check if the files are already available and also the dependencies with other scripts. You can first use wp_register_style to register the style file and wp_enquieue_style to include the file. However, make sure to use the admin_enqueue_script action instead of wp_enqueue_sript for the admin side.
#7. Creating Shortcodes
It is inevitable for plugin developers to learn about shortcodes – the predefined blocks of codes that can be used anywhere; since shortcodes allow you to add dynamic behavior to custom pages. You can not only create shortcode using syntax, but also assign a shortcode name and function to the add_shortcode function. You can then return the type of content you want to display in the browser inside the function.
The aforementioned pointers are just a few of the common things one needs to know during the process of plugin creation. We will share more in our next article; till then we look forward to you sharing your concepts on the same.