WordPress.org

Ready to get started?Download WordPress

Forums

Stripping comment HTML before inserted into database (4 posts)

  1. Henry
    Member
    Posted 11 months ago #

    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?

  2. Bob Cristello
    Member
    Posted 11 months ago #

    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.

    Bob

    Original Source: http://lawsonry.com/2013/06/disable-html-in-wordpress-comments.html

  3. Henry
    Member
    Posted 11 months ago #

    Thanks Bob, i'll give that a try.

  4. Henry
    Member
    Posted 11 months ago #

    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' );

Reply

You must log in to post.

About this Topic