How to Remove the Unset URL Field from the WordPress Comment Form

Home » Snippets » How to Remove the Unset URL Field from the WordPress Comment Form
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

If you maintain a WordPress site that allows comments, you may have noticed that the comment form includes a URL field. However, this can lead to spam comments or misuse by commenters trying to self-promote their own sites. In this article, we’ll show you how to remove the URL field from the comment form so that you can improve the quality of the comments on your site. Let’s dive in!

					function WPTurbo_remove_comment_url_field( $fields ) {
    unset( $fields['url'] );
    return $fields;
}
add_filter( 'comment_form_default_fields', 'WPTurbo_remove_comment_url_field' );
				

In this article, we will show you how to remove the URL field from the WordPress comment form. This can be useful if you don’t want to allow commenters to enter their website URLs and avoid spam comments. The code snippet for achieving this is given below:

function WPTurbo_remove_comment_url_field( $fields ) {
    unset( $fields['url'] );
    return $fields;
}
add_filter( 'comment_form_default_fields', 'WPTurbo_remove_comment_url_field' );

The code defines a new function called WPTurbo_remove_comment_url_field() that accepts the $fields parameter, which contains an array of HTML fields for the comment form. We then use the unset() function to remove the URL field from the array using its key url.

The unset() function removes the element from the array, but it does not reindex it. The remaining elements maintain their original keys. Lastly, we return the modified $fields array from the function.

In the final step, we add a filter to the comment_form_default_fields hook, passing our WPTurbo_remove_comment_url_field() function to remove the URL field from the comment form. This ensures that the URL field is not displayed on the comment form on the WordPress website.

By following the above steps, you can easily remove the URL field from the comment form on your WordPress website.

Register an account to save your snippets or go Pro to get more features.