Useful Snippets

Welcome!


This blog is used to collect useful snippets related to Linux, PHP, MySQL and more. Feel free to post comments with improvements or questions!

Are your smart devices spying on you? Make better purchasing choices and find products that respect your privacy at Unwanted.cloud

RSS Latest posts from my personal blog


Subscribe to RSS feed


Send email when a post is sent for review (pending) in WordPress

Stanislav KhromovStanislav Khromov

Here’s a quick snippet for sending an email when a post is sent for review. (Post status: Pending) This happens when a Contributor writes a post. Since they don’t have the publish_post capability, their post will have the status of “pending”. (Do you want to send email when a new post is published? Read this post instead!)

add_action('future_to_pending', 'send_emails_on_new_event');
add_action('new_to_pending', 'send_emails_on_new_event');
add_action('draft_to_pending', 'send_emails_on_new_event');
add_action('auto-draft_to_pending', 'send_emails_on_new_event');

/**
 * Send emails on event publication
 *
 * @param WP_Post $post
 */
function send_emails_on_new_event($post)
{
    $emails = "email_1@mail.com, email_2@mail.com"; //If you want to send to site administrator, use $emails = get_option('admin_email');
    $title = wp_strip_all_tags(get_the_title($post->ID));
    $url = get_permalink($post->ID);
    $message = "Link to post: \n{$url}";

    wp_mail($emails, "New post published: {$title}", $message);
}    

This will result in an email like this:

[Title] New post published: Your Post

Link to post:
http://site.com/your-post/

If you need to do this for a specific post type only you can have a conditional inside the filter function:

...
if(get_post_type($post->ID) === 'page') //post, page, attachment or whatever other CPT you may have
{
    //use wp_mail() here!
}
...

Full-stack impostor syndrome sufferer & Software Engineer at Schibsted Media Group

Comments 0
There are currently no comments.