wordpress 如果用户填写了自定义字段,我如何添加检查并不发送对某些评论的自动回复?

fzwojiic  于 2023-02-11  发布在  WordPress
关注(0)|答案(1)|浏览(107)

我在评论表单中使用自定义字段“你喜欢这本书吗?”和“是”和“否”值。我想发送2个不同的自动回复评论,如果用户选择“是”,那么响应是“谢谢你的善意认可,客户的满意永远是我们的目标。”如果“不”,那么自动回答是“我们将努力做得更好”。我将非常感谢您的任何帮助...

//And this is how I send an auto reply to a comment
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;
    } 
    //How do I add a response if the comment contains the value of the [likebook ] meta field???
    if( !empty( $_POST['likebook'] ) {
return;
    }
    
    $commentdata = [
        'comment_post_ID'      => $commentdata['comment_post_ID'],
        'comment_author'       => 'admin',
        'comment_author_email' => 'admin@example.com',
        'comment_author_url'   => 'http://example.com',
        'comment_content'      => 'Thank you for your kind recognition, customer satisfaction is always our goal.',
        'comment_type'         => 'comment',
        'comment_parent'       => $comment_ID,
        'user_ID'              => 1,
    ];
  
    wp_new_comment( $commentdata );
}
osh3o9ms

osh3o9ms1#

试试这些,

add_action( 'comment_post', 'author_new_comment', 10, 3 );
function author_new_comment( $comment_ID, $comment_approved, $comment ) {
    if ( $comment['comment_parent'] !== 0 ) {
        return;
    }
    
    if ( ! empty( $_POST['like'] ) ) {
        if ( $_POST['like'] == 'dog' ) {
            $msg = 'We are glad that you liked it!';
        } else {
            $msg = 'We will try to make this product better!';
        }
        
        $admins       = get_super_admins();
        $current_user = get_user_by( 'login', $admins[0] );
        
        $data = array(
            'comment_post_ID'      => $comment['comment_post_ID'],
            'comment_content'      => $msg,
            'comment_parent'       => $comment_ID,
            'user_id'              => $current_user->ID,
            'comment_author'       => $current_user->user_login,
            'comment_author_email' => $current_user->user_email,
            'comment_author_url'   => $current_user->user_url
        );
        
        $comment_id = wp_insert_comment( $data );
        if ( is_wp_error( $comment_id ) ) {
            throw new Exception( $comment_id );
        }
    }
}

相关问题