Home > Net >  Wordpress comment_text() missing <p> tags
Wordpress comment_text() missing <p> tags

Time:10-10

I'm creating my own WordPress Bootstrap theme and have run in to a strange issue.

When I submit a comment and it's waiting for moderation, comment_text() does not output any <p> tags.

I've tried disabling all the plugins and renaming my functions.php file and the issue persists.

My code :

<div ><?php comment_text(); ?></div>

Approved comment ( has <p> tags ):

<div id="comment-10" >
<div >
<div >
<div >Barry says</div>
<div ><a href="https://example.com/test/#comment-10" >September 11, 2022 at 8:06 am</a></div>
<div ><p>Testing to see how this comment looks</p></div>

Waiting for moderation ( no <p> tags ):

<div id="comment-59" >
<div >
<div >
<div >Dave says</div>
<div ><a href="https://example.com/test/#comment-59" >October 5, 2022 at 8:14 pm</a></div>
<p >Your comment is awaiting moderation.</p>
<div >ok how does this look</div>

Also, when I enable the subscribe to comments reloaded plugin, the comment looks like this :

<div id="comment-63" >
<div >
<div >
<div >Ken says</div>
<div ><a href="https://example.com/test/#comment-63" >October 5, 2022 at 8:45 pm</a></div>
<p >Your comment is awaiting moderation.</p>
<div >Check your email to confirm your subscription.
Ok this doesn't look right
</div>

Looking at the source code for the plugin, they also add <p> tags for their message, but those are getting removed as well.

I'm at a loss to what the issue could be. Any suggestions?

CodePudding user response:

I finally found the solution to the problem.

The WordPress function wp_kses in Walker_Comment::filter_comment_text() strips all <p> tags by default (<p> tag is not listed in the "$allowedtags" array).

https://github.com/WordPress/wordpress-develop/blob/6.0.2/src/wp-includes/kses.php#L379-L413

To solve this issue, I only wanted to allow <p> tags in comments so I added the following function inside my Bootstrap_Comment_Walker extends Walker_Comment class :

public function filter_comment_text( $comment_text, $comment )
{
    $allowedtags = array(
            'p' => array(),
    );
    return wp_kses( $comment_text, $allowedtags );
}

Everything is working as expected now.

  • Related