Coding, Wordpress

How to Create a Custom WordPress Alert Plugin

WordPress Alert Plugin

WordPress plugins are a great way of customizing your site by adding features and services to suit your needs. They allow us to add functionality to WordPress without interacting with the core code directly. It is also easy for the user to activate or deactivate the features at will.
Although there are many plugins that are available for download over the internet, you may have a unique requirement that the available plugins fail to satisfy. The cost of hiring a web developer to make the custom plugin for you is quite high; learning how to do it yourself could therefore save you a lot of money. The demand for custom plugins is ever high and this creates an opportunity to earn a decent income if you learn how to make them. This tutorial will guide you through all the steps of making a plugin that will be recognized by WordPress. A good understanding of PHP is a requirement.

In this example, we will create a simple plugin that will be sending an email to a specified address whenever a new post is added to our blog. This could be used to notify the site admin when other authors upload content to the WordPress blog.

Requirements:

  • You will need to have a WordPress blog to test your plugin on. This can either be a WordPress installation on your local drive or a hosted blog. It is advisable to test plugins in a local WordPress installation to minimize the risk of your site going down in case of errors in your code.
  • You will also need your favorite PHP IDE or any text editor (notepad is basically good enough) to write your code on.

Step 1: Create a folder in the ‘plugins’ folder of the blog site and give it the same name as the plugin. The ‘plugins’ folder under ~yoursite/wordpress/wp-content/plugins/. This allows WordPress to recognize the plugin.
A plugin is made up of at least a README.txt file to describe what it does in details and the PHP file that contains the program code. It is only good to put these files in a single folder to maintain a good organization.

Step 2: Write the php code and save it with a .php extension in the folder created in step 1. In this case, we will save it as Published Alert.php.

The php code for our plugin has 3 parts:
The first part of the code should be the plugin header. This holds information about your plugin and enables WordPress to recognize your code as a plugin. This should be presented in a standard way as follows:

/*
Plugin Name: Published Alert!
Plugin URI: http://coderewind.com/
Description: Automatically sends an email to a specified address when a new post is published
Author: CodeRewind 
Version: 1.0
Author URI: http://coderewind.com/
*/

You should choose a plugin name that highlights what the plugin does. The name should however be unique in case you want to share the plugin in WordPress plugin directory.
The header will make the plugin to be viewable under installed plugins on the administrator dashboard of your website. From this menu, you can activate/deactivate the plugin or edit the plugin code.

alert plugin installed

The Plugin URI is an address to the plugins webpage where users can obtain more information and help about the plugin. The version number should be chosen appropriately to accommodate for future releases of the same plugin. All the information that is specified in the header will be viewable from the ‘installed plugins’ menu in the admin dashboard.
The second part of the plugin code specifies what the plugin does. We should therefore create the PHP function that sends an email to the admin email address.

function alert_admin() {//creates a function and names it alert_admin
    mail('admin@yoursite.com', 'A new article has been posted on the blog!', 'This is a notification that a new post has been added');
}
add_action('publish_post', 'alert_admin()'); 
?>

mail() is an inbuilt PHP function that takes three strings as parameters. The first parameter is the recipient email address, the second string is the email header, and the third is the message body.

The third part of the php code is the tag which hooks our plugin to the WordPress engine. In short, we are going to specify when the plugin will be executed.

add_action('publish_post', 'alert_admin()'); 

This creates a hook using the inbuilt add_action function. The first argument is the action (or the tag) for which the alert_admin function that we created will be executed.
The clean php code for our plugin will be:


Adding Options
Our plugin will this far be able to do the desired task of sending an email notifications when a post is published. We however should add a few options that will enable the user to change some aspects of the plugin on the dashboard- using plugin settings. We will therefore provide our user with a way to specify the admin email address, the message Header and the message body.
We need to modify the code a little so that parameters can be changed rather than hard coding them as in our previous version of the plugin.

/*
Plugin Name: Published Alert!
Plugin URI: http://coderewind.com/
Description: Automatically sends an email to a specified address when a new post is published
Author: CodeRewind 
Version: 1.0
Author URI: http://coderewind.com/
*/

function alert_admin() {
	$target=get_option('alert_target');
	$header=get_option('message_header');
	$body=get_option('message_body');
	mail($target,$header,$body);
	}
function set_alert_options() {
	add_option('alert_target','Hello','what say');
	add_option('message_header','new Alert','what say');
	add_option('Message_body','Body','The contents in the body');
	}
function unset_alert_options() {
	delete_option('alert_target');
	delete_option('message_header');
	delete_option('message_body');	
	}
register_activation_hook(_FILE_.'set_alert_options');
register_deactivation_hook(_FILE_.'set_alert_options');

We begin by modifying the alert_admin function so that the target, header and the body of the message can be set by the option that the user will set. We accomplish this using the inbuilt get_option method. We then create two functions to set and unset the options in the target, message header and body. The last two methods are responsible for registering the plugin to WordPress when the user activates it and cleaning up the database when the user deactivates it.

We then have to add an interface on the admin panel from which the user can select/enter his preferred options. This is achieved by modifying the menu on the admin dashboard and adding an options page using add_options_page function as shown.

function modify_menu(){
	add_options_page('Published Alert Options',//The title of the page
					'Alert Options', //The sub-title of the page
					'manage_options',	//capability
					'_FILE_',	//the file to be used
					'admin_alert_options'	//function
					);
}

This function adds the menu named “Alert Options” under settings on the admin panel.

Alter Plugin Menu

We should also have a function that will handle the options that the user inputs from the interface. It listens for an action on the submit/save button on the admin interface and calls the update_alert_options function to effect the changes.

function admin_alert_options(){
		if($_REQUEST['submit']) {
		update_alert_options();
	}
	print_alert_form();
}

The update_alert_options function should do the real job of taking the user inputs and assigns them to the alert_target, message_header and message_body of our plugin.

function update_alert_options(){
	$ok=false;
	//email validation can be added here
	if ($_REQUEST['alert_target']){
		update_option('alert_target', $_REQUEST['alert_target']);
		$ok=true;
	}
	if ($_REQUEST['message_header']){
		update_option('message_header', $_REQUEST['message_header']);
		$ok=true;
	}
	if ($_REQUEST['message_body']){
		update_option('message_body', $_REQUEST['message_body']);
		$ok=true;
	}
	if($ok){
		echo 
		"";
	}
	else{
		echo 
		"";
	}}

The final step involves creating the form that the user will use to input the settings. This is the interface of our program. We will use an ordinary HTML form and use a function that will print the form (with the current settings) whenever it is called.

function print_alert_form(){
	Set Your Alert Preferences:
	$default_target = get_option('alert_target');
	$default_header = get_option('message_header');
	$default_body = get_option('message_body');
	?>
   	




}

This will yield the HTML form shown below.

Alert Plugin Form

We now have a WordPress plugin that allows the user to change the settings.

Step 3: The third step in creating a WordPress plugin involves creating a readme file and saving it in the folder created in step 1.
Placing the plugin in the plugins directory under ~yoursite/wordpress/wp-content/plugins will automatically make it viewable on the admin panel. You can also put the plugin files into a zip file to enable installation from the admin panel. This can be done by selecting plugins-> Add New-> Upload. You can then Browse the .zip file from your local disk and install it using the Install Now button. This will automatically install your plugin and make it viewable in the list of installed plugins.

Install WordPress Alert Plugin

After you have tested your plugin, you may consider submitting it to the WordPress plugin directory to share it to the WordPress community. The plugin should have a unique name when it is uploaded to the WordPress directory for a unique identity. Subsequent releases with updated features or fixes should retain the original name with a different but appropriate version number.

You can download the complete source of this plugin from here WordPress Alert Plugin

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.