探索建站和运维乐趣

两个实现限制WordPress评论内容长度方法 防止长篇留言

我们有些朋友在使用WordPress程序的时候,对于用户交互留言功能还是比较挑剔的。不过我们会发现,默认的留言内容有字数的限制或者是可以提供的留言内容太长,导致页面模板被撑开影响其他的用户体验。所以,我们最好设置WordPress评论留言内容的长度,我们可以通过下面两个方法实现。

第一、无插件用代码实现

add_filter( 'preprocess_comment', 'wpb_preprocess_comment' );
    function wpb_preprocess_comment($comment) {
        if ( strlen( $comment['comment_content'] ) > 3000 ) {
            wp_die('Comment is too long. Please keep your comment under 3000 characters.');
        }
    if ( strlen( $comment['comment_content'] ) < 60 ) {
            wp_die('Comment is too short. Please use at least 60 characters.');
        }
        return $comment;
    }

默认是60个字符,我们 可以根据需要设置。完毕之后将代码丢到当前主题Functions.php文件中。

第二、Control Comment Length插件

这里我们也可以使用"Control Comment Length"插件来解决这个问题,搜索插件安装。

根据上图所示,我们可以设置最少和最多字符数量。

赞(0)
转载保留:老部落 » 两个实现限制WordPress评论内容长度方法 防止长篇留言


关注公众号『老蒋朋友圈』

获取更多建站运营运维新知!
互联网创业、前沿技术......