Quotes Of The Day: WordPress Plugin Development

Quotes Of The Day: WordPress Plugin Development
Quotes Of The Day: WordPress Plugin Development

Previously, we have covered the basics of WordPress plugin development. This is the 3rd series where we will start a new project and cover some important topics related to the WordPress plugins.

Plugin Idea

We will create a simple Quotes of the day plugin which will pop some shortlisted quotes every time a user visits the website.

We will slowly add additional functionality and styles with customized options

WordPress Plugin Setup

Create a new plugin directory inside your WordPress site and name anything you like for the above plugin idea. I will name it Quotes of the day.

If you are first time here directly landed on this blog post, visit the introduction part of WordPress plugin development:

Once you have created your new plugin directory, implement the header requirements as described in our introduction part and create a class for your plugin.

<?php
/**
* Plugin Name: Quotes Of The Day
* Plugin URI: https://www.ashiish.me/
* Description: Everyday is a quote day, welcome your users with a quotes every time they visit your website.
* Version: 1.0.0
* Author: Ashish Yadav
* Author URI: https://www.ashiish.me
* License: GPLv2 or later
* Text Domain: quotesoftheday
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*
* @package QuotesOfTheDay
*/
if(!defined('ABSPATH')) {
die; // Die if accessed directly.
}
/**
* CONSTANTS
* @since 1.0.0
*/
// PLUGIN FILE
if(!defined('QUOTESOFTHEDAY_PLUGIN_FILE')) {
define('QUOTESOFTHEDAY_PLUGIN_FILE', __FILE__);
}
/**
* QuotesOfTheDay Class
* @class QuotesOfTheDay
*/
class QuotesOfTheDay {
// Constructor
function __construct() {
// nothing yet
}
}
if(class_exists('QuotesOfTheDay')) {
$quotesoftheday = new QuotesOfTheDay();
}

In the above code, you can see I have defined a plugin file path QUOTESOFTHEDAY_PLUGIN_FILE which we will use in the future.

So, now let’s talk about some core functionality of WordPress plugins. WordPress provides us some hooks for the plugin which will be very helpful to plugin developers to perform certain actions.

Activation / Deactivation Hooks

Whenever you install a plugin, you can find the list of installed plugins in the plugin section of the WordPress dashboard. You can see an option to activate or deactivate if the plugin is already activated.

So, it is important to look into these activation and deactivation actions so that we can specify a certain task to be done or stopped when a plugin is activated & deactivated.

When to use Activation / Deactivation Hook?

Let’s say we have a plugin that requires another plugin in the WordPress site in order to work. So, you can use the activation hook to check if the plugin exists and show some error message if it doesn’t.

/**
* QuotesOfTheDay Activation Hook
* @since 1.0.0
*/
function quotesoftheday_activation_hook() {
//wp_die("Activation Hook");
}
register_activation_hook(QUOTESOFTHEDAY_PLUGIN_FILE, 'quotesoftheday_activation_hook');

For deactivation hook, let’s say your plugin saves some cache data of a website. This hook will be very helpful when a user chooses to deactivate your plugin for a certain time and you can flush the cache when your plugin is deactivated.

/**
* QuotesOfTheDay Deactivation Hook
* @since 1.0.0
*/
function quotesoftheday_deactivation_hook() {
//wp_die("Deactivation Hook");
}
register_deactivation_hook(QUOTESOFTHEDAY_PLUGIN_FILE, 'quotesoftheday_deactivation_hook');

Note: Never use deactivation hooks to delete the data saved by your plugin. A plugin can be activated again and you will lose all the data saved by a user. Data deletion can be done when a plugin is uninstalled.

Uninstall WordPress Plugin

There is another hook provided by WordPress for plugin development. Uninstall hooks is triggered when a user deactivates your plugin and click the delete button.

Uninstall hook is used for cleanup purposes. Let us suppose your plugin saves tons of data into the WordPress database. When a user uninstalls the plugin, it is a good practice to clean up any data stored by your plugin. You can trigger a function and remove all the data or settings saved when your plugin is removed.

<?php
/**
* QuotesOfTheDay Uninstall
*
* @since 1.0.0
* @package QuotesOfTheDay
*/
// If not called by WordPress
if(!defined('WP_UNINSTALL_PLUGIN')) {
die;
}
// Clear any cached data that has been removed.
wp_cache_flush();
view raw uninstall.php hosted with ❤ by GitHub

All the above code for WordPress Activation, Deactivation or Uninstallation hooks is demo code. We will not use it on the plugin that we are going to build.

We will leave these hook empty for a while and only use it when necessary.

You can find the updated code on the repository below.

WordPress Plugin: Quote Of The Day

If you have any questions or need help to achieve some functionality using the hooks explained above, let me know in the comment section.

Thanks for reading!