Home / Disable / Remove hyperlink from name field
Duplicate Snippet

Embed Snippet on Your Site

Remove hyperlink from name field

Pete Tran
<10
Code Preview
php
<?php
add_filter('preprocess_comment', 'remove_links_from_comment_author');
function remove_links_from_comment_author($commentdata) {
    // Regular expression to detect hyperlinks
    $pattern = '/<a href\s*=\s*"[^"]+">([^<]+)<\/a>/i';
    // Check if the comment author's name contains a hyperlink
    if (preg_match($pattern, $commentdata['comment_author'])) {
        // Remove the hyperlink
        $commentdata['comment_author'] = preg_replace($pattern, '$1', $commentdata['comment_author']);
        
        // Or you can choose to block the comment by doing something like:
        // wp_die('Hyperlinks are not allowed in the name field.');
    }
    return $commentdata;
}

Comments

Add a Comment