Comments are a great way to engage with your website visitors and build a community around your content.
However, sometimes comments can become a source of spam and unwanted URLs. As a website owner, you may want to disable links in comments to prevent such issues.
Fortunately, WordPress provides multiple ways to disable links in comments.
In this tutorial, we will guide you through disabling all types of Links in comments in WordPress using manual code addition.
Convert Links To Plain Text For All Users (Including Admin)
To create a rule that will convert any link added in the comments to plain text for all users, including admin:
1. Open your WordPress dashboard and go to Appearance > Theme Editor.
2. Click on the “functions.php” file to edit it.
3. Add the following code at the bottom of the file:
remove_filter( 'comment_text', 'make_clickable', 9 );
4. Click on the “Update File” button to save your changes.
The above code will remove the make_clickable
filter from the comment_text
filter hook, with a priority of 9
.
The make_clickable
filter is a WordPress filter that converts URLs and email addresses in a string of text into clickable links.
By removing this filter from the comment_text
hook, the URLs and email addresses in comments will no longer be converted into clickable links. And this change is for all users, including admins.
Convert Links To Plain Text For Front-end Users (Not Admin)
Now, if you want to convert the links to plain text for front-end users only and allow only administrators to post links in the comments, then follow the below steps:
1. Open your WordPress dashboard and go to Appearance > Theme Editor.
2. Click on the “functions.php” file to edit it.
3. Add the following code at the bottom of the file:
function allow_make_clickable_for_admin($content) { if ( !current_user_can('manage_options') ) { remove_filter( 'comment_text', 'make_clickable', 9 ); } return $content; } add_filter( 'the_content', 'allow_make_clickable_for_admin' );
4. Click on the “Update File” button to save your changes.
This code checks if the current user has the manage_options
capability, which is typically only given to administrators. If the user is not an administrator, it removes the make_clickable
filter from the comment_text
hook, which disables the link creation feature in comments for front-end users.
However, if the user is an administrator, the filter will not be removed, allowing administrators to create links in their comments.
Display Error Message When Link in The Comment Is Detected
The above two methods will only convert the link to plain text (i.e., the links will not be clickable).
However, if you want to completely disable the comments that contain any kind of links and show an Error message like in the below image:
Then use the below code that will block comments containing links and display an error message to the user:
1. Open your WordPress dashboard and go to Appearance > Theme Editor.
2. Click on the “functions.php” file to edit it.
3. Add the following code at the bottom of the file:
add_filter( 'preprocess_comment', 'disable_comment_url' ); function disable_comment_url( $commentdata ) { if ( !is_user_logged_in() ) { $comment_content = $commentdata['comment_content']; if ( preg_match( '/(https?|ftp):\/\/[^\s\/$.?#].[^\s]*/i', $comment_content ) ) { wp_die( 'Error: Links are not allowed in comments.' ); } } return $commentdata; }
4. Click on the “Update File” button to save your changes.
With the above code, a user will be redirected to an error page when he tries to add a comment containing a link.
The above code uses the preprocess_comment
filter to check if a comment contains a link, and if it does, it blocks the comment and displays an error message to the user using the wp_die()
function. If the comment doesn’t contain a link, it allows the comment to be posted.
Note: As an admin, you will still be able to post comments with links as long as you’re logged in. This code only applies to non-logged-in users.
Conclusion
Disabling links in comments can help prevent spam and unwanted links from being posted on your website. However, you don’t necessarily need to add all the code snippets mentioned in this tutorial. You can choose the one that fits your needs best.
Whichever code you use, it’s important to always make a backup of your functions.php file before making any changes.
If you still have any questions or need further assistance, feel free to comment with your queries below. We’re here to help!