0

I have this in my functions.php

/*Remove website field from comment*/
function xyzmark_remove_url($fields) {
    if(isset($fields['url'])){ unset($fields['url']); }
    return $fields;
}
add_filter('comment_form_default_fields', 'xyzmark_remove_url');

Below is screenshot of comment form. It has no website url field as intended no url in wp comment

But I still get url when someone post comment. Below is screenshot of email I got still url in comment

Not sure how to completely disabled this comment field so that I can get rid of spammers posting links in comment form url field.

Note: I am using custom coded theme.

2 Answers 2

1

The code you've provided does remove the website URL field from the comment form. However, bots can still directly POST to the wp-comments-post.php file with a URL included. To prevent this, you can remove the URL from the submitted data before it's saved to the database. You can achieve this using the preprocess_comment filter.

Add the following code to your theme's functions.php file:

function xyzmark_remove_comment_author_url( $commentdata ) {
    $commentdata['comment_author_url'] = ''; // remove the URL from the submitted data
    return $commentdata;
}
add_filter('preprocess_comment', 'xyzmark_remove_comment_author_url');

This code will ensure that even if someone (or a bot) directly submits a comment with a URL, it will be removed before the comment data is saved. Note that this won't affect the ability of admins to manually add URLs to comments from within the WordPress admin interface.

2
  • I didn't know about preprocess_comment filter. This will certainly help me to get rid of those urls in my admin. However this doesn't discourage a spammer to post comments with url. Since a commenter/spammer don't know that the url is being stripped. Is there a way to completely stop the form submission if the spammer post url along with other detail? (and optionally show them a error/failed message)? My point is to discourage spammer from posting url. Jun 27, 2023 at 10:58
  • @sanjayojha For me it's better if you don't show an error message if a URL is posted. It falls under the principle of "security through obscurity," where the spammers remain unaware that their efforts are ineffective, thus potentially reducing their attempts to circumvent your system. Jan 23 at 12:43
1

This is what worked for me for my GeneratePress site.

function xyzmark_remove_comment_author_url($commentdata) {
    // Remove the URL from the submitted data
    $commentdata['comment_author_url'] = '';
    return $commentdata;
}

// Hook the function to the 'preprocess_comment' filter
add_filter('preprocess_comment', 'xyzmark_remove_comment_author_url', 10, 1);

function xyzmark_remove_url($fields) {
    // Check if the 'url' field exists in the $fields array and unset it if it does
    if (isset($fields['url'])) {
        unset($fields['url']);
    }
    return $fields;
}

// Hook the function to the 'comment_form_default_fields' filter
add_filter('comment_form_default_fields', 'xyzmark_remove_url');

// Additional code to remove the URL field from the HTML output
function xyzmark_remove_comment_url_input($html) {
    $html = preg_replace('/<input.*?id="url".*?>/i', '', $html);
    return $html;
}

// Hook the function to the 'comment_form_field_url' filter
add_filter('comment_form_field_url', 'xyzmark_remove_comment_url_input');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.