Attach to comment () to add surrounding tag

Trying to connect to the comment_text () function provided by the Wordpress API to wrap the output of each comment in a container, <div>...</div>I encountered the following problem:

Without my extra filter, the output of comment_text () looks like this:

<p>Hello User!</p>
<p>Thank you for your comment.</p>
<p>Stefan</p>

OK, but as I said, I would like it to be wrapped in <div class="comment-text">...</div>. As far as I know, the correct way to do this is to add a filter to functions.phpmy topic, and so I did:

function stefan_wrap_comment_text($content) {
    return "<div class=\"comment-text\">". $content ."</div>";
}
add_filter('comment_text', 'stefan_wrap_comment_text');

As I can see in the output, this filter works, but it has a negative effect on the first paragraph of the content, as you can see in the following example. The first paragraph should be <p>Hello User!</p>, but it looks like this: Hello User!.

<div class="comment-text">
    Hello User!
    <p>Thank you for your comment.</p>
    <p>Stefan</p>
</div>

Any ideas or hints on what I'm doing wrong?

+3
source share
2 answers

Try to lower the priority of your function, maybe there is some formatting function that you precede.

add_filter('comment_text', 'stefan_wrap_comment_text', 1000);
+6
source

Ouch, just stumbled upon a file wp-includes/default-filters.phpand found out that for each function several filters are applied by default:

add_filter( 'comment_text', 'wptexturize'            );
add_filter( 'comment_text', 'convert_chars'          );
add_filter( 'comment_text', 'make_clickable',      9 );
add_filter( 'comment_text', 'force_balance_tags', 25 ); 
add_filter( 'comment_text', 'convert_smilies',    20 );
add_filter( 'comment_text', 'wpautop',            30 );

30 wpautop(), <p>...</p>. add_filter() 10. , , , .

// This doesn't work because default priority is 10:
// add_filter('comment_text', 'stefan_wrap_comment_text');
// Add a lower priority (higher number) to apply this filter at last: 
add_filter('comment_text', 'stefan_wrap_comment_text', 99);
+5

Source: https://habr.com/ru/post/1740483/


All Articles