Home > Software engineering >  How to auto reply to a comment wordpress?
How to auto reply to a comment wordpress?

Time:10-30

I'm trying to create an auto reply/thank you for reviews in my bookstore... but without success... I would appreciate any help!

add_action( 'comment_post', 'author_new_comment', 10, 2 );
function author_new_comment( $comment_ID, $comment_approved, $commentdata ){
    // exit if the comment is not approved
    //if( $comment_approved == 0 )
    //  return;
$comm = get_comment( $comment_ID );
$commentdata = [
    'comment_post_ID'      => 2978,
    'comment_author'       => 'Admin',
    'comment_author_email' => '[email protected]',
    'comment_author_url'   => 'http://example.com',
    'comment_content'      => 'Andy, thanks for your review of my book',
    'comment_type'         => 'comment',
    'comment_parent'       => '$comm',
    'user_ID'              => 0,
];
    
    // add Database
wp_new_comment( $commentdata );
}

CodePudding user response:

Try something like this:

add_action( 'comment_post', 'author_new_comment', 10, 3 );
function author_new_comment( $comment_ID, $comment_approved, $commentdata ){
    $comment_parent = (int) $commentdata['comment_parent'];
    
    // If a new comment is a reply to another comment, don't do anything
    if ( $comment_parent !== 0 ) {
        return;
    }
    
    $commentdata = [
        'comment_post_ID'      => $commentdata['comment_post_ID'],
        'comment_author'       => 'admin',
        'comment_author_email' => '[email protected]',
        'comment_author_url'   => 'http://example.com',
        'comment_content'      => 'Andy, thanks for your review of my book',
        'comment_type'         => 'comment',
        'comment_parent'       => $comment_ID,
        'user_ID'              => 1,
    ];
  
    wp_new_comment( $commentdata );
}
  • Related