questions

Notify guest author by email on WordPress website

Rate this post

I am using a plugin called User Submitted Posts on a WordPress website. The plugin creates a form by using a shortcode. When a user fills out the form, the submitted information is used to create a post. The name submitted through the form is stored in the database as a guest-author. The email submitted is stored as user_submit_email.

The problem I’m having is that the guest author doesn’t receive notifications when someone comments on their post. I’m trying to figure out how to set it up so that notifications are sent to the guest author via email without the website owner having to do a manual action on each post submission to set it up.

The code below is what I found from the User Submitted Posts plugin. I located it in the functions.php file in the website’s child theme.

add_filter( 'the_author', 'guest_author_name' );
add_filter( 'get_the_author_display_name', 'guest_author_name' );
 
function guest_author_name($name) {
global $post;
 
$author = get_post_meta( $post->ID, 'guest-author', true );
 
if ( $author )
$name = $author;
 
return $name;
}

 

✔️Solution:

You need to hand this in an action hook, not a filter. If you want to handle a user submitting a comment, you use the wp_insert_comment_hook.

Assuming you stored post meta guest_author, you’d do something like

function wpr_authorNotification( $comment_id, $comment_object) {
    $post = get_post($comment_object->comment_post_ID);

    if ( ! isset($post->guest_author) ) {
        return;
    }
    $message = "";
    $subject = "";;
    wp_mail($post->guest_author, $subject, $message);
}

add_action('wp_insert_comment', 'wpr_authorNotification', 99, 2);

 

Leave a Reply

Your email address will not be published.

Back to top button