Skip to content

Instantly share code, notes, and snippets.

@laxmariappan
Last active July 14, 2022 10:25
Show Gist options
  • Save laxmariappan/df8803497d29b03ef77e51452111c3c0 to your computer and use it in GitHub Desktop.
Save laxmariappan/df8803497d29b03ef77e51452111c3c0 to your computer and use it in GitHub Desktop.
Set error message to post meta.
<?php function set_notification_status( $post_id, $post ) {
if ( ! $post ) {
return; // Exit if there is no response.
}
// @see https://developer.wordpress.org/reference/hooks/new_status_post-post_type/#comment-4335
$author = $post->post_author;
$name = get_the_author_meta( 'display_name', $author );
$email = get_the_author_meta( 'user_email', $author );
$title = $post->post_title;
$permalink = get_permalink( $post_id );
$to[] = sprintf( '%s <%s>', $name, $email );
$subject = sprintf( 'Published: %s', $title );
$message = sprintf( 'Congratulations, %s! Your article "%s" has been published.' . "\n\n", $name, $title );
$message .= sprintf( 'View: %s', $permalink );
$headers[] = '';
$sent = wp_mail( $to, $subject, $message, $headers );
if ( $sent ) {
$response = array(
'code' => 'success',
'message' => 'Email notification(s) sent sucessfully',
);
} else {
$response = array(
'code' => 'error',
'message' => 'Failed to send email notification(s)',
);
}
update_post_meta( $post_id, 'email_notification', wp_json_encode( $response ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment