Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WordPress自定义注释字段-验证_Php_Wordpress - Fatal编程技术网

Php WordPress自定义注释字段-验证

Php WordPress自定义注释字段-验证,php,wordpress,Php,Wordpress,我在WordPress中使用了一个自定义评论模板,用户可以在其中留下评论/评分等。我试图做的是验证其中一个字段是否为空 以下是我的功能: function website_verify_comment_review() { if (empty($_POST['review-stars-input'])) { wp_die(esc_html__('Error: Please add a rating with your comment.', 'website

我在WordPress中使用了一个自定义评论模板,用户可以在其中留下评论/评分等。我试图做的是验证其中一个字段是否为空

以下是我的功能:

function website_verify_comment_review() {
        if (empty($_POST['review-stars-input'])) {
            wp_die(esc_html__('Error: Please add a rating with your comment.', 'website'));
    }
}
我正在我的functions.php中使用“pre_comment_on_post”钩子,因为我似乎无法在其他任何地方添加此操作,除非它不起作用

add_action('pre_comment_on_post', 'website_verify_comment_review');
我遇到的问题是,这也在普通帖子上运行,而这些自定义注释字段不存在,因此错误也显示在那里

我需要这个函数在过滤器中运行,只在我的自定义帖子类型上运行,但它似乎不起作用。在我上面的函数中,我尝试检查它是否不是一个单独的post“!is_singular('post')”,但这使它不再适用于我的自定义post类型

也许有人知道怎么了?为什么这只在我从functions.php添加操作时起作用

我还尝试过调整add_操作优先级,但没有成功

谢谢

empty()将在值为null或不存在时返回true,因此您可以测试该值是否存在且是否为空(只是想一想):


您好,欢迎来到stackoverflow,谢谢您的回答。虽然这段代码可能会回答这个问题,但是你能考虑为你解决的问题增加一些解释,以及你是如何解决的?这将帮助未来的读者更好地理解你的答案并从中学习。
if ( isset( $_POST['review-stars-input'] ) && empty( $_POST['review-stars-input'] ) ) {
// your error message
}
function website_verify_comment_review($post_id) {
    if (get_post_type($post_id) == 'Your post type here') {
        if (empty($_POST['review-stars-input'])) {
            wp_die(esc_html__('Error: Please add a rating with your comment.', 'website'));
        }
    }
}