I'm using a custom comment_type for my plugin and that comment type is connected to only a custom post_type. And now I want to add the TinyMCE Editor to the comment section, and want to allow more tags than typical, especially some headings, pre tag, and ordered & unordered lists. But adding an editor to the comment section is the half part of it. I need to allow those tags to bypass the wp_kses for the comment content.
I did something earlier, that seems foolish now:
$response_msg = wp_kses( $response_msg, my_allowed_html() );
$comment_id = wp_new_comment( array(
'comment_post_ID' => absint( $post->ID ) ,
'comment_author' => wp_strip_all_tags( $current_user->display_name ),
'comment_author_email' => sanitize_email( $current_user->user_email ),
'comment_author_url' => esc_url( $current_user->user_url ),
'comment_content' => $response_msg,
'comment_type' => 'wpse_response',
'comment_parent' => 0,
'user_id' => absint( $current_user->ID ),
) );
Because even though the my_allowed_html()
function passes newly allowed HTML to the comment section, the wp_new_comment()
, with its inherited filter will wp_kses()
all of 'em to the defaults. So that's foolish.
I figured out that this solution is not a good one, as of now (WP v5.2.1) comment_post
would be too late to hook for this. The correct code for doing this would be what Milan Petrovic suggested:
function wpse20190615_allowed_html_in_responses($comment_content)
{
global $allowedtags;
$allowedtags['pre'] = array('class'=>array());
$allowedtags['h2'] = array();
$allowedtags['h3'] = array();
$allowedtags['h4'] = array();
$allowedtags['h5'] = array();
$allowedtags['h6'] = array();
$allowedtags['ul'] = array();
$allowedtags['ol'] = array();
$allowedtags['li'] = array();
return $comment_content;
}
add_filter( 'pre_comment_content', 'wpse20190615_allowed_html_in_responses', 9 );
But what I understand is, this filter will allow these HTML tags into all the comments, including the default comments for posts, pages. I want to allow these HTML tags restricted to only the comments of my comment_type (or my post type).
I tried with the init
hook but seems global $post
and $post->post_type
detection might be too earlier, so eventually not worthy for my case.
And pre_comment_content
allows only a single parameter and that doesn't have the information about the comment type also.
So, how can I restrict the code above to a certain comment_type, something like:
function wpse20190615_allowed_html_in_responses($comment_content)
{
// if('wpse_response' !== $comment->comment_type) {
// return $comment_content;
// }
global $allowedtags;
...
return $comment_content;
}
add_filter( 'pre_comment_content', 'wpse20190615_allowed_html_in_responses', 9 );
I'm using a custom comment_type for my plugin and that comment type is connected to only a custom post_type. And now I want to add the TinyMCE Editor to the comment section, and want to allow more tags than typical, especially some headings, pre tag, and ordered & unordered lists. But adding an editor to the comment section is the half part of it. I need to allow those tags to bypass the wp_kses for the comment content.
I did something earlier, that seems foolish now:
$response_msg = wp_kses( $response_msg, my_allowed_html() );
$comment_id = wp_new_comment( array(
'comment_post_ID' => absint( $post->ID ) ,
'comment_author' => wp_strip_all_tags( $current_user->display_name ),
'comment_author_email' => sanitize_email( $current_user->user_email ),
'comment_author_url' => esc_url( $current_user->user_url ),
'comment_content' => $response_msg,
'comment_type' => 'wpse_response',
'comment_parent' => 0,
'user_id' => absint( $current_user->ID ),
) );
Because even though the my_allowed_html()
function passes newly allowed HTML to the comment section, the wp_new_comment()
, with its inherited filter will wp_kses()
all of 'em to the defaults. So that's foolish.
I figured out that this solution is not a good one, as of now (WP v5.2.1) comment_post
would be too late to hook for this. The correct code for doing this would be what Milan Petrovic suggested:
function wpse20190615_allowed_html_in_responses($comment_content)
{
global $allowedtags;
$allowedtags['pre'] = array('class'=>array());
$allowedtags['h2'] = array();
$allowedtags['h3'] = array();
$allowedtags['h4'] = array();
$allowedtags['h5'] = array();
$allowedtags['h6'] = array();
$allowedtags['ul'] = array();
$allowedtags['ol'] = array();
$allowedtags['li'] = array();
return $comment_content;
}
add_filter( 'pre_comment_content', 'wpse20190615_allowed_html_in_responses', 9 );
But what I understand is, this filter will allow these HTML tags into all the comments, including the default comments for posts, pages. I want to allow these HTML tags restricted to only the comments of my comment_type (or my post type).
I tried with the init
hook but seems global $post
and $post->post_type
detection might be too earlier, so eventually not worthy for my case.
And pre_comment_content
allows only a single parameter and that doesn't have the information about the comment type also.
So, how can I restrict the code above to a certain comment_type, something like:
function wpse20190615_allowed_html_in_responses($comment_content)
{
// if('wpse_response' !== $comment->comment_type) {
// return $comment_content;
// }
global $allowedtags;
...
return $comment_content;
}
add_filter( 'pre_comment_content', 'wpse20190615_allowed_html_in_responses', 9 );
Share
Improve this question
edited Jun 15, 2019 at 0:54
fuxia♦
107k39 gold badges255 silver badges459 bronze badges
asked Jun 14, 2019 at 20:38
Mayeenul IslamMayeenul Islam
12.9k21 gold badges85 silver badges169 bronze badges
1 Answer
Reset to default 2This is untested but should work since it gives you the data you need to work from and runs prior to the pre_comment_content
filter where the comment is sanitized.
add_filter( 'preprocess_comment', 'wpse_340526_preprocess_comment' );
function wpse_340526_preprocess_comment( $commentdata ) {
if( 'wpse_response' === $commentdata['comment_type'] ){
global $allowedtags;
$allowedtags['pre'] = array('class'=>array());
$allowedtags['h2'] = array();
$allowedtags['h3'] = array();
$allowedtags['h4'] = array();
$allowedtags['h5'] = array();
$allowedtags['h6'] = array();
$allowedtags['ul'] = array();
$allowedtags['ol'] = array();
$allowedtags['li'] = array();
}
return $commentdata;
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745400151a4626062.html
评论列表(0条)