You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress中comment_text()待审核评论缺失<p>标签问题求助

解决待审核评论comment_text()丢失

标签的问题

原因分析

WordPress对待审核评论会应用更严格的内容过滤规则,默认可能移除了<p>这类HTML标签,导致comment_text()输出时没有段落格式。

解决办法

  • 手动添加过滤钩子恢复

    标签
    把这段代码加到主题的functions.php中,针对未审核评论重新调用wpautop生成段落标签:

    add_filter('comment_text', 'preserve_p_tags_for_unapproved_comments', 99);
    function preserve_p_tags_for_unapproved_comments($comment_text) {
        $comment = get_comment();
        if (!$comment->comment_approved) {
            $comment_text = wpautop($comment_text);
        }
        return $comment_text;
    }
    
  • 调整未审核评论的允许标签列表
    修改WordPress对未审核评论的过滤规则,明确保留<p>标签:

    add_filter('pre_comment_content', 'allow_p_tags_in_unapproved_comments');
    function allow_p_tags_in_unapproved_comments($content) {
        $comment = get_comment();
        if (isset($comment->comment_approved) && !$comment->comment_approved) {
            $allowed_tags = wp_kses_allowed_html('post');
            $allowed_tags['p'] = array();
            return wp_kses($content, $allowed_tags);
        }
        return $content;
    }
    
  • 绕过comment_text()手动处理内容
    如果钩子方式无效,可以直接获取评论内容并手动处理格式:

    <div class="comment-content my-4">
        <?php 
        $comment = get_comment();
        $content = $comment->comment_content;
        if (!$comment->comment_approved) {
            $content = wpautop($content);
            $content = wp_kses($content, wp_kses_allowed_html('post'));
        }
        echo $content;
        ?>
    </div>
    

内容的提问来源于stack exchange,提问作者Brian Smith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 12:25:31