0

I need to send the WordPress new post notification to an email address stored in a custom field value.

I know how to get other meta data from the db using get_the_author_meta. What I do not know is where the changes have to be done in WordPress send mail trigger.

1

I think this would be possible using the wp_mail() function (https://developer.wordpress.org/reference/functions/wp_mail/) alongside the publish_post action.

In fact, the publish_post action codex page has an example:

https://codex.wordpress.org/Plugin_API/Action_Reference/publish_post

function post_published_notification( $ID, $post ) {
    $author = $post->post_author; /* Post author ID. */
    $name = get_the_author_meta( 'display_name', $author );
    $email = get_the_author_meta( 'user_email', $author );
    $title = $post->post_title;
    $permalink = get_permalink( $ID );
    $edit = get_edit_post_link( $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[] = '';
    wp_mail( $to, $subject, $message, $headers );
}
add_action( 'publish_post', 'post_published_notification', 10, 2 );

This could be modified to get the email address from a custom field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.