Comment form validation

Comments processing is done in the file: wp-comments-post.php. You can use the hook pre_comment_on_post to validate the values entered in the comment form fields.

function custom_validate_comment_url() {
    if( !empty( $_POST['url'] ) && !preg_match( '\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|]', $_POST['url'] ) // do you url validation here (I am not a regex expert)
        wp_die( __('Error: please enter a valid url or leave the homepage field empty') );
}

add_action('pre_comment_on_post', 'custom_validate_comment_url');

if you want to change a submitted value, use the filter preprocess_comment. E.g.:

function custom_change_comment_url( $commentdata ) {
    if( $commentdata['comment_author_url'] == 'Your homepage' )
        $commentdata['comment_author_url'] = '';
    return $commentdata;
}

add_filter('preprocess_comment', 'custom_change_comment_url');

Leave a Comment