JANUARY PROMOTION: Only $17 get Bonsai WordPress theme
LEARN MORE
Hello everyone! It must have been a long time since we last met each other on this series of WordPress Tutorial. For fear that you guys might forget about me, today I’m making a come-back. As this is an important event, I have been thinking really hard about the topic for this come-back, and realized nothing is better than giving some tutorial as well as some encouraging words to those enthusiastic, hard-working people of the WordPress community. Guess who I am talking about! Actually, you guys can see it right from the title! Yes, let use this article to honor our WordPress Plugin Developers, the ultimate creators of all amazing plugins we are using at the moment!
No one can ever doubt the importance of plugins nowadays. Everyone who uses the Internet can sense how vital it is to have some plugins. Especially us, WordPress users. We must know that thanks to these plugins, WordPress is able to power millions of blogs and websites, including ours.
Having the skills needed to extend WordPress to satisfy all the needs is not something easy. Understanding this hardship, I have consulted with several masters of WordPress plugin development I know in order to write this WordPress Tutorial for you. This article consists of some handy tips for every WordPress plugin developer. If you have just come to the game, this article is a must-read.
Now, let’s get our journey started, will we?
Whatever you do, remember to enable debugging and leave it working the entire time you write a plugin. This may alert you when things go wrong by displaying error messages and warnings. It also lets you know if you use any deprecated functions. So, do not underestimate this step.
Because debugging is turned off by default, you have to turn it on first. Before instructing you how to do it, I must suggest making a copy of the wp-config.php so that you can always go back to it if something goes wrong. Then, it’s time to open the wp-config.php in the root of WordPress installation. Check if you see this line:
define(‘WP_DEBUG’, false);
If you do, replace it with the following lines:
// Turns WordPress debugging on
define(‘WP_DEBUG’, true);
// Tells WordPress to log everything to the /wp-content/debug.log file
define(‘WP_DEBUG_LOG’, true);
// Doesn’t force the PHP ‘display_errors’ variable to be on
define(‘WP_DEBUG_DISPLAY’, false);
// Hides errors from being displayed on-screen
@ini_set(‘display_errors’, 0);
After adding those lines, you can be assured that your debugging is now turned on.
It would be useless if you enabled debugging yet doing nothing more, right?! So let me lead to another stage of debugging, that is logging your own debug statements. There are quite many ways to achieve the end result, but here I will show you the simplest way. This way is to use echo and see the message on the page. Although this measure can be considered a dirty-hack way to debug, it is indeed quick and it is used by everyone. However, if you feel guilty, there is another way which is to create a function. This function helps you do the job, and let you see all of your own debug statements in the debug.log file.
Here’s that function you can utilize:
function log_me($message) {
if (WP_DEBUG === true) {
if (is_array($message) || is_object($message)) { error_log(print_r($message, true));
} else {
error_log($message);
}
}
}
After that, feel free to call the log_me function like below:
log_me(array(‘This is a message’ => ‘for debugging purposes’));
log_me(‘This is a message for debugging purposes’);
Short codes are amazing in making your end users feel satisfied with your plugin. Provided that you are already familiar with WordPress, you must have run into short codes once or twice. Its basic form is the key word between square brackets. What do short codes do literally? They provide customers with ease. So, if you can master the use of short codes in your plugins, chances are that they will return to you for future projects.
This is not about short codes, but just codes. Well, somewhere along the development path of your plugin, you may wish to insert code into other files. This can help maintaining your plugin turn an easier job. For example, people usually create a functions.php file that has all of the common functions.
Let’s take an example, if you name your main plugin file myplugin.php and you want to create the functions.php file. Then, add these lines to it:
include ‘functions.php’; include_once ‘functions.php’; require ‘functions.php’; require_once ‘functions.php’;
However, you should really distinguish between the 4 popular codes, that is include, include_once, require, and require_once. In this way, you can choose to use one among them based on the outcome you expect:
If you have been writing WordPress plugin for some time, I bet you should know that updates are unavoidable under any circumstances. In many cases, you will be confused when experiencing the upgrade. So is there any way to make it as convenient as possible? Yes there is, indeed.
Keep the plugin version is what. By this way you can always read it later to decide on the level of upgrading for your plugin. How to do it? Well, all you need is to create some more variables and call on theadd_option() function:
if (!defined(‘MYPLUGIN_VERSION_KEY’)) define(‘MYPLUGIN_VERSION_KEY’, ‘myplugin_version’);
if (!defined(‘MYPLUGIN_VERSION_NUM’)) define(‘MYPLUGIN_VERSION_NUM’, ‘1.0.0’);
add_option(MYPLUGIN_VERSION_KEY, MYPLUGIN_VERSION_NUM);
And when it’s time to check if you need to perform upgrade actions, your plugin will show codes that look like this:
$new_version = ‘2.0.0’;
if (get_option(MYPLUGIN_VERSION_KEY) != $new_version) {
// Execute your upgrade logic here
// Then update the version value
update_option(MYPLUGIN_VERSION_KEY, $new_version);
}
In this WordPress Tutorial, besides keeping up with the upgrades of your own plugin, you are also recommended to take into consideration all kinds of WordPress development. As a part of this community, this is definitely your duty and right. For example, it is advisable to keep a close watch on upcoming new WordPress version release. At the same time, check if there are any vulnerabilities in the version you are using. Where to check up on those information? Development blogs, of course.
In this regard, may I introduce to you some WordPress websites and blogs that will not only update you on WordPress news, but also give you precious tips and hacks. Those include WordPress Beginner and WordPress Tavern.
You can activate any plugin by clicking Activate Link presented in the plugin list. While a simple plugin will not require you to do any activation, an advanced one will make you carry out stuff like initializing plugin options, creating plugin tables, etc. In this step I will show you how to handle plugin activation and deactivation.
In fact, WordPress has a function called register_activation_hook. This function is triggered when the plugin is activated. However, we can add a custom function to bring plugin activation into effect by adding the lines below:
function wp_sample_activation() {
}
register_activation_hook(__FILE__, ‘wp_sample_activation’);
Please note that you will have go through the path of the file containing the activation function being the first parameter and the function name being the second one.
In case the activation function lies inside the main plugin file, you are advised to use __FILE__ as shown in the code. With this, you can do tasks like validations, initializations and table creations within the activation function.
For deactivation of a plugin, we can use the register_deactivation_hook, . With the same syntax as for activation, you can tidy up the plugin resources, options and tables within the deactivation function.
function wp_sample_deactivation() {
}
register_deactivation_hook(__FILE__, ‘wp_sample_deactivation’);
A WordPress Tutorial cannot be called one if it doesn’t tell you important pieces of information. One of them is that WordPress supplies us with a default set of such fields as title, content, image, and excerpt all laid out in the content creation screen. To add additional behavior, we need custom fields. To be specific, well, we need to supplement separate fields if checkboxes are required, as well as radio buttons, drop downs and such.
In order to create option boxes to add additional fields, do as follows:
add_action(‘add_meta_boxes’, ‘add_custom_fields_box’);
function add_custom_fields_box() {
add_meta_box(‘custom_fields_box_id’, ‘Custom Info’, ‘display_custom_info_box’, ‘post’, ‘normal’, ‘high’);
} function display_custom_info_box() {
global $post;
$html = “<table><tr><td>Custom Checkbox</td><td><input id=’custom_checkbox’ type=’checkbox’ name=’custom_checkbox’ /></td></tr> <tr><td>Custom Selecy</td><td><select name=’custom_select’ > <option>Option 1</option> </select></td></tr> <tr><td>Custom Upload</td><td><input id=’custom_file’ type=’file’ name=’custom_file’ /></td></tr></table>”;
echo $html;
}
So, here’s the end of the ultimate WordPress Tutorial for WordPress Plugin Developers! To advanced developers, I hope that you have at least found something useful or taken something into experience after reading my post. As for those who have just stepped into the industry, do not worry if things get complicated a little bit while reading this. As long as you are strong enough to get through, you will become the master very soon.
Have any more tips for WordPress plugin developers? Let us know in the comment section, please!