admin管理员组

文章数量:1126455

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

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

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.

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

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

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.

Share Improve this question asked Jun 25, 2023 at 4:19 sanjay ojhasanjay ojha 1851 silver badge8 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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.

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');

本文标签: How to remove website url field from comment form completely