Support » Fixing WordPress » Stripping comment HTML before inserted into database

  • Henry


    add_filter( 'comment_text', 'wp_filter_nohtml_kses' );

    This strips all HTML tags from the comment at the point just before it is displayed on screen. In this situation both the comment text and any HTML tags sit in the database.

    Instead, I’d like to strip all HTML tags from the comment before it is inserted into the database. So, the comment_text tag isn’t what I’m looking for.

    Anyone know which tag I should use?

Viewing 3 replies - 1 through 3 (of 3 total)
  • I believe this solution will help you. Just before the closing ?> PHP tag in your functions file, add the following code:

    // Create a function to call when a comment is posted
    function plc_comment_post( $incoming_comment ) {
    // Next, use PHP's htmlspecialchars() function to convert the comment to plain text...
    $incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']);
    // ... except for single quotes, which we cannot convert to its ASCI equivalent (which is #039)
    // because WP will mark the comment as spam (oh no!)
    $incoming_comment['comment_content'] = str_replace( "'", ''', $incoming_comment['comment_content'] );
    // return the cleaned up comment variable for WP to use
    return( $incoming_comment );
    // This will occur before a comment is displayed
    function plc_comment_display( $comment_to_display ) {
    // Put the single quotes back in
    $comment_to_display = str_replace( ''', "'", $comment_to_display );
    return $comment_to_display;

    Good luck with your project.


    Original Source:



    Thanks Bob, i’ll give that a try.



    I was looking in core and came across pre_comment_content

    Would this be a good approach?

    add_filter( 'pre_comment_content', 'wp_filter_nohtml_kses' );

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Stripping comment HTML before inserted into database’ is closed to new replies.