Using post status transitions to send custom alerts (in WP Job Manager)

Something I get asked a fair bit with regards to my plugins is how to send an alert during an event, for example, “when a job expires in WP Job Manager, how can I notify the user?”

Plugins which use custom post types, like WP Job Manager, are likely to use custom/standard post statuses too – it’s pretty common. Using these statuses you can easily hook-in a custom function and trigger and email.

This is a quick tutorial for hooking into a status change and sending an email for WP Job Manager.

Create a custom plugin, open up functions.php, or create a drop in

You need to place your code somewhere. I’d recommend a plugin or drop in to keep your code upgrade-safe, but you can also use the theme functions.php file. You can read about making plugins in the codex – I’ll focus on the code itself rather than this aspect.

Let’s create a class to house our functionality:

class My_Job_Manager_Custom_Alerts {
/**
* Constructor
*/
public function __construct() {
}
}

view raw

gistfile1.txt

hosted with ❤ by GitHub

Find out the status you wish to act upon and hook into it

Job Manager uses WP core statuses plus 1 custom one called ‘expired’ (which is registered using the register_post_status function).

Generally, the flow goes something like:

  1. Draft (draft)
  2. Pending (pending)
  3. Published (publish)
  4. Expired (expired)

In this example we want to send an alert out when a published job expires, so the post status transition hook will be called publish_to_expired. WordPress automatically triggers this hook when necessary.

The hook/code will look like this:

class My_Job_Manager_Custom_Alerts {
/**
* Constructor
*/
public function __construct() {
add_action( 'publish_to_expired', array( $this, 'expired_post' ) );
}
/**
* Expired post triggered by publish_to_expired hook
* @param $post post object
*/
public function expired_post( $post ) {
// DO THINGS HERE
}
}

view raw

gistfile1.txt

hosted with ❤ by GitHub

When the hook is triggered, the expired_post method will be called and passed a post object (the post which has had it’s status modified).

Check the post object

We must remember, this hook can be triggered by any post, not just a job. Let’s check it’s type:

if ( 'job_listing' == $post->post_type ) {
// This is a job
}

view raw

gistfile1.txt

hosted with ❤ by GitHub

Create your email alert content

We’ve got a trigger now, which also passes the post. Now we can use this to create the actual alert email, and send it using wp_mail.

Content wise, lets have this read:

Hello,

The job “{job_title}” which you posted on {sitename} has just expired and will no longer be visible.

To check your other active listings, or post a new job listing, please visit your dashboard.

Putting this into the code we’ll have this:

$message = sprintf( "Hello,\n\nThe job “%s” which you posted on %s has just expired and will no longer be visible.\n\nTo check your other active listings, or post a new job listing, please visit your dashboard.", esc_html( $post->post_title ), esc_html( get_bloginfo( 'name' ) ) );

view raw

gistfile1.txt

hosted with ❤ by GitHub

Get the email address to notify

Two outcomes here; we can either send to the registered user, or the application email. If the user is a guest and provided neither, we can send nothing.

This code gets the application email (if set) from the listing, or if it’s not a guest submission, the user email.

$send_to = '';
$email = get_post_meta( $post->ID, '_application', true );
$user = $post->post_author;
if ( is_email( $email ) ) {
$send_to = $email;
} elseif ( $user ) {
$user = get_user_by( 'id', $user );
$send_to = $user->user_email;
}
if ( ! $send_to )
return;

view raw

gistfile1.txt

hosted with ❤ by GitHub

Send it

Now we just need to send the email. wp_mail makes this simple.

wp_mail( $send_to, 'Your job listing has expired', $message );

view raw

gistfile1.txt

hosted with ❤ by GitHub

All together now

Here is the final code combined and ready to go:

class My_Job_Manager_Custom_Alerts {
/**
* Constructor
*/
public function __construct() {
add_action( 'publish_to_expired', array( $this, 'expired_post' ) );
}
/**
* Expired post triggered by publish_to_expired hook
* @param $post post object
*/
public function expired_post( $post ) {
if ( 'job_listing' == $post->post_type ) {
$send_to = '';
$email = get_post_meta( $post->ID, '_application', true );
$user = $post->post_author;
if ( is_email( $email ) ) {
$send_to = $email;
} elseif ( $user ) {
$user = get_user_by( 'id', $user );
$send_to = $user->user_email;
}
if ( ! $send_to )
return;
$message = sprintf( "Hello,\n\nThe job “%s” which you posted on %s has just expired and will no longer be visible.\n\nTo check your other active listings, or post a new job listing, please visit your dashboard.", esc_html( $post->post_title ), esc_html( get_bloginfo( 'name' ) ) );
wp_mail( $send_to, 'Your job listing has expired', $message );
}
}
}
new My_Job_Manager_Custom_Alerts();

view raw

gistfile1.php

hosted with ❤ by GitHub

And here is what you’ll receive:

2013-12-24 at 17.44