How to Set a Comment as Pending in WordPress if Author Link Exceeds 50 Characters

Home » Snippets » How to Set a Comment as Pending in WordPress if Author Link Exceeds 50 Characters
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Comments are a great way to engage with your website visitors and get feedback on your posts. However, not all comments are created equal – some may contain links to spammy or irrelevant websites. As a website owner, you may want to review these comments before they are published on your site. In this article, we’ll show you how to set comments as pending if the author link in the comment is greater than 50 characters, helping you to control the quality of comments on your WordPress site.

					function wpturbo_pending_comment_links( $commentdata ) {
    if ( strlen( $commentdata['comment_author_url'] ) > 50 ) {
        $commentdata['comment_approved'] = 0;
    }
    return $commentdata;
}
add_filter( 'preprocess_comment', 'wpturbo_pending_comment_links' );
				

In this tutorial, we are going to show you how to set a comment as pending if the author’s link is greater than 50 characters. This code snippet will be especially useful in preventing spam comments with long links from appearing on your website.

The code snippet above defines a new function called wpturbo_pending_comment_links(). This function takes the incoming comment data as a parameter and checks if the length of the comment author’s URL is greater than 50 characters. If the condition is true, the function sets the comment’s comment_approved field to 0, which means the comment will be marked as pending.

function wpturbo_pending_comment_links( $commentdata ) {
    if ( strlen( $commentdata['comment_author_url'] ) > 50 ) {
        $commentdata['comment_approved'] = 0;
    }
    return $commentdata;
}

The strlen() function is used to calculate the length of the comment author’s URL. If the length of the URL is greater than 50 characters, the comment_approved field is set to 0, which effectively sets the status of the comment to pending.

if ( strlen( $commentdata['comment_author_url'] ) > 50 ) {
    $commentdata['comment_approved'] = 0;
}

Finally, we need to add a filter to WordPress using the add_filter() function. We attach our wpturbo_pending_comment_links() function to the preprocess_comment filter, which runs just before a comment is saved to the database. This ensures that our function is executed and can modify the comment data as needed.

add_filter( 'preprocess_comment', 'wpturbo_pending_comment_links' );

By adding this code snippet, you can prevent spam comments on your WordPress website that have long links. The code checks the length of the author’s URL, and if it is greater than 50 characters, the comment is set to pending, which requires manual approval from the website’s administrator.

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