09/10/2017 / Samuel

This article explains how to create a plugin that sends an e-mail whenever a post is deleted. It takes advantage of Action Hooks, a feature in WordPress.
 

Intro to Action Hooks

To put that simply, an action hook is an instruction that triggers a function when a specific WordPress task is done. For example the code below will call the send_urgent_email() function when a plugin is activated. activated_plugin Runs each time a plugin is successfully activated. You can think of action hooks as event listeners in Javascript (eg: onClick()).

// The function to hook
function send_urgent_email() {
	// Instructions to send the e-mail
}

// Setting up the hook
add_action( 'activated_plugin', 'send_urgent_email');

 

Creating the plugin

In order to create the skeleton of a plugin, create any folder within the /wp-content/plugins directory and add a php file. It is recommended to name the file according to the plugin name, in lowercase and without special characters. In our case, the plugin is Trash Monitor, the folder will be trash-monitor and the file name trash-monitor.php as illustrates the picture below.

Overview of the plugin folder structure
Overview of the plugin folder structure

Then, insert the comments below in the header of the php file. In short, this comment part will be the “ID card” of the plugin, and will register the folder as a WordPress plugin.

<?php
/**
 * Plugin Name: Trash Monitor
 * Plugin URI: https://github.com/samuelguebo
 * Description: This plugin sends an e-mail whenever a post is deleted.
 * Version: 0.0.1
 * Author: Samuel Guebo
 * Author URI: https://github.com/samuelguebo
 * License: GPL3
 * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
 */
 ?>

If you correctly followed the steps above, your plugin should appear in the plugins list. Activate it.

Plugin using Action hook
The plugin is displayed in the dashboard.

Now, the hook

As we mentioned earlier, our goal is to send an e-mail whenever someone deletes or moves a post to the trash. Let’s first set up the skeleton by adding the code below into our plugin.

// The function to hook
function send_urgent_email() {
	// Instructions to send the e-mail
}

// Setting up the hook
add_action( 'activated_plugin', 'send_urgent_email', 10, 3);

 

We used the code from the introduction but changed the action from activated_plugin to transition_post_status. transition_post_status is the action performed when the status of a post changes, for example when it goes from published to trashed (deleted).

The transition_post_status action needs 3 parameters:

  • $new_status, the new status of the WordPress post
  • $old_status, the previous status before being trashed
  • $post, the post object

And add_action() takes the following parameters:

add_action( string $tag, 
	callable $function_to_add, 
	int $priority , 
	int $accepted_args
)

Next let’s expand and finalize the code above.

<?php 
/**
 * Plugin Name: Trash Monitor
 * Plugin URI: https://github.com/samuelguebo
 * Description: This plugin sends an e-mail whenever a post is deleted.
 * Version: 0.0.1
 * Author: Samuel Guebo
 * Author URI: https://github.com/samuelguebo
 * License: GPL3
 * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
 */

 function send_urgent_email( $new_status, $old_status, $post ) {
	// If the post went from being published to trashed
    if ( $old_status == 'publish'  &&  $new_status == 'trash' ) {
        
		// The e-mail parameters
		$object   = $post->post_title . " was deleted!";
		$to  = get_bloginfo('admin_email');
		$headers[] = "From: " .get_bloginfo('title'). " <$to>";
		
		// Concatenate the message
		$message  = "";
		$message .= "The post " . $post->post_title;
		$message .= " was deleted!";
		
		// Send the e-mail
		wp_mail( $to, $object, $message, $headers );

	} // End if
}

// Setting up the hook
add_action( 'transition_post_status', 'send_urgent_email', 10, 3 );
?>

 

Conclusion

We quickly explored how to create a minimalist plugin skeleton and dived into Action Hooks. By now you should have a clear understanding of Action Hooks. There is more to say particularly if you intend to build a plugin with more advanced features. So, if you wish to dive deeper, I recommend this introduction to plugin development from WPMU and the excellent tutorial series of Tom McFarlin about Object-Oriented Programming in WordPress.
 
Photo credit: webhouseit.com

Facebook Comments