Created
June 21, 2025 02:45
-
-
Save khoipro/d25e32d9300c6eb9d6eaeb175e85668f to your computer and use it in GitHub Desktop.
Reject any comment contain URLs, disable comment field URL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Sử dụng: paste đoạn code vào trong file theme functions.php | |
// Không cần nếu bạn đã tắt comment trên web | |
function codetot_prevent_urls_in_comment_content( $commentdata ) { | |
$comment_content = $commentdata['comment_content']; | |
$url_pattern = '/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/i'; | |
if ( preg_match( $url_pattern, $comment_content ) ) { | |
wp_die( | |
__( '<strong>ERROR</strong>: Your comment contains a URL. Please remove it before submitting.', 'your-text-domain' ), | |
__( 'Comment Submission Error', 'codetot' ), | |
array( | |
'response' => 200, | |
'back_link' => true | |
) | |
); | |
} | |
return $commentdata; | |
} | |
add_filter( 'preprocess_comment', 'codetot_prevent_urls_in_comment_content' ); | |
function codetot_remove_comment_website_field( $fields ) { | |
if ( isset( $fields['url'] ) ) { | |
unset( $fields['url'] ); | |
} | |
return $fields; | |
} | |
add_filter( 'comment_form_default_fields', 'codetot_remove_comment_website_field' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment