• Resolved tmeche87

    (@tmeche87)


    Hey!

    I am trying to sanitize any comments that are being written on my site. After researching, I believe I need to use the wp_new_comment function to do this. However, I am unsure as to where I should be using this function at.

    Also, I am trying to do the same for some some custom types which have custom fields. I have created custom templates to display these custom fields (i.e. single-project.php). I want to sanitize the custom fields, but once again, I am unsure as to where I should be using the sanitation functions at.

    P.S. I am using the Twenty Eleven theme if that makes any difference.

    Thanks a bunch for any help!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter tmeche87

    (@tmeche87)

    Also, I used the Types plugin to create my custom types and fields.

    Moderator keesiemeijer

    (@keesiemeijer)

    Try it with the ‘preprocess_comment’ filter. It is run just before the comment gets inserted into the database. Example (in your theme’s functions.php):

    add_filter( 'preprocess_comment', 'my_comment_sanitize' );
    function my_comment_sanitize( $comment_arr ) {
    
    	// change comment content to 'hello world'
    	$comment_arr['comment_content'] = 'hello world';
    
    	return  $comment_arr;
    }

    These are the values you can sanitize with this filter:

    // $comment_arr
    
    Array
    (
        [comment_post_ID] => 3315
        [comment_author] => keesiemeijer
        [comment_author_email] => [email protected]
        [comment_author_url] =>
        [comment_content] => This is the best post I've seen in a long time
        [comment_type] =>
        [comment_parent] => 0
        [user_ID] => 13
    )

    Thread Starter tmeche87

    (@tmeche87)

    Thanks for the help Keesie. Going through the code in my functions.php file, I think it actually already sanitizes the comments for me (although I’m not positive lol). Regardless, thanks for the help. You have taught me something new!

    Thanks again Keesie! You’re the man!

    Moderator keesiemeijer

    (@keesiemeijer)

    You’re welcome.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Sanitize comments and custom fields’ is closed to new replies.