Countermeasures Against AI Comment Spam in WordPress

In recent years, the spread of generative AI has significantly changed the methods used for blog comment spam. Traditional spam comments mostly consisted of unnatural machine-translated Japanese or boilerplate text completely unrelated to the article’s content. However, an increasing number of spam comments now use AI to read the article text, generate a natural-sounding review, and subtly slip a link to an external site at the end.

Even for our blog’s article on the terminal splitting tool, we received comments that accurately picked up specific terms from the article (such as “byobu’s Screen mode", “Ctrl + a", and “pane splitting"). At first glance, these seemed like feedback from a reader who had carefully read the article; however, because multiple comments with similar author names and identical structures arrived, we decided to scrutinize the contents.

Extracting a portion of the comment, the flow looks like the following (the destination URL has been masked):

“For those who regularly perform pane splitting and parallelize tasks, I think it’s a big deal to see a path where you don’t have to abandon the tool. (omitted) As related information, I felt that tools from different fields, such as social flyer maker (link to external site), share a similar concept in terms of making work visual."

What makes judgment even more difficult is the safety of the destination link. When we manually ran the listed URL through a link checker, no security issues such as malware or phishing were detected. Because an actual, harmless web tool page was being used, simple dangerous-site checks alone made it difficult to distinguish whether the comment was made in good faith or was spam.

目次

Advertisement

Preventing Abuse Through Backlink-Driven SEO Spam

The background behind such posts—despite their natural phrasing and harmless link destinations—is the objective of acquiring backlinks to boost search rankings (SEO spam). The fact that they force a connection from a topic about terminal operations to an unrelated tool via a vague commonality like “making work visible" allows us to conclude that these are automated posts designed to place links on specific keywords.

To prevent blogs from being used as a stepping stone for SEO spam and to maintain a healthy comment section, countermeasures must be implemented from both operational rules and output control perspectives.

Restricting Comments with Links and Ensuring the Application of Invalidation Attributes

This countermeasure aims to strictly enforce operations where comments containing external links are not published without administrator review, while ensuring that even if links are output in the comment section, the rel="nofollow ugc" attribute is applied to all links without exception.

Deceptive Tactics and Loopholes in WordPress Standard Features

The basics of spam countermeasures are “do not approve suspicious comments." However, when AI-generated natural reviews are combined with harmless link destinations, the risk increases that administrators will mistakenly approve them as good-faith comments.

An even greater problem is bypassing WordPress’s standard link invalidation feature. WordPress inherently possesses a feature that automatically adds the search-engine-oriented invalidation attribute rel="nofollow ugc" to links in the comment section. However, upon checking the HTML source of the actual spam comment received, the rel attribute was not applied at all, as shown below (the URL has been replaced with an example dummy).

<p>(omitted) As related information, <a href="https://example.com/social-flyer-maker/
">social flyer maker</a>, I felt that tools from different fields also share a similar concept in terms of making work visible.</p>

Checking the problem link, there is a line break immediately before the closing quotation mark of the href attribute. Because WordPress’s standard automatic assignment process is based on string replacement using regular expressions, when a line break is sandwiched inside an HTML tag like this, or when the active theme or plugins overwrite the attributes, the process can fail, resulting in a raw link being output.

Enforcing Approval Rules and Adding Code to functions.php

To simultaneously prevent erroneous approvals due to misjudgment and the bypassing of standard features, we will combine basic operational settings with adding code to functions.php.

Basic Countermeasure: Do Not Approve Comments Containing Suspicious URLs

Comments introducing external tools or services unrelated to the main topic of the article will not be approved—even if the link destination is a safe site—and will be excluded as spam. If similar writing styles or resembling author names continue, it is safe to assume they are automated posting bots.

Additionally, open “Discussion" from the administrative dashboard’s “Settings", and change the link count criteria under the “Comment Moderation" section from the default value of “2" to “1". This ensures that any comment containing even a single URL or link will automatically be held for moderation.

Technical Countermeasure: Effects of Adding nofollow ugc and Implementation Code

In addition to operational countermeasures, ensuring that rel="nofollow ugc" is added to links in the comment section yields the following three effects:

  1. Disabling the Transfer of SEO Value
    By adding the nofollow and ugc (User Generated Content) attributes, you can clearly signal to search engines that “this is not a link recommended by the administrator." This blocks the spammer’s objective of boosting search rankings.
  2. Protecting Your Site’s Search Rating and Credibility
    If normal links are posted to external sites unrelated to the blog’s theme, your site may be evaluated as one that leaves low-quality outbound links or spam unchecked. Forcibly adding the attributes minimizes adverse effects on your own site.
  3. Reducing the Likelihood of Becoming a Target for Spam Bots
    By making it recognized as a site where placing links yields no SEO benefit, you eliminate the incentive for them to continually target you with spam posts.

As insurance against bypassing standard features, we implement a process that forcibly adds rel="nofollow ugc" at the final stage of outputting HTML to the public page. By using the WP_HTML_Tag_Processor—an HTML parsing class built natively into WordPress—instead of regular expressions, attributes can be accurately assigned regardless of line breaks inside tags.

Add the following code to the end of your active child theme’s functions.php. Note that incorrect entries in functions.php can cause errors across the entire site and lock you out of the administrative dashboard; therefore, always make a backup of the file before working, and edit the child theme rather than the parent theme so your changes aren’t erased during theme updates.

/**
 * Ensure rel="nofollow ugc" is reliably added when outputting links within comments
 */
function donguri_force_comment_nofollow_ugc( $html ) {
\tif ( empty( $html ) || ! is_string( $html ) || false === stripos( $html, '<a' ) ) {
\t\treturn $html;
\t}

\tif ( ! class_exists( 'WP_HTML_Tag_Processor' ) ) {
\t\treturn $html;
\t}

\t$processor = new WP_HTML_Tag_Processor( $html );

\twhile ( $processor->next_tag( 'a' ) ) {
\t\t$rel_attr = $processor->get_attribute( 'rel' );
\t\t$rels     = is_string( $rel_attr )
\t\t\t? preg_split( '/\s+/', trim( $rel_attr ), -1, PREG_SPLIT_NO_EMPTY )
\t\t\t: array();

\t\t$rels_lower = array_map( 'strtolower', $rels );

\t\tforeach ( array( 'nofollow', 'ugc' ) as $required ) {
\t\t\tif ( ! in_array( $required, $rels_lower, true ) ) {
\t\t\t\t$rels[] = $required;
\t\t\t}
\t\t}

\t\t$processor->set_attribute( 'rel', implode( ' ', $rels ) );
\t}

\treturn $processor->get_updated_html();
}
add_filter( 'comment_text', 'donguri_force_comment_nofollow_ugc', 999 );
add_filter( 'get_comment_author_link', 'donguri_force_comment_nofollow_ugc', 999 );

Because this code sets the filter priority to 999, it executes at the final stage after the theme or other plugins have manipulated the attributes. It targets not only comment bodies (comment_text) but also author name links (get_comment_author_link), supplementing only the missing nofollow and ugc without erasing existing rel attributes. Additionally, normal comments containing no links execute an early return to minimize unnecessary processing load.

HTML Output and Operational Changes After Adding the Code

After adding the code to the child theme’s functions.php and clearing the cache, we checked the output results of the target comment, and the HTML source had changed as follows:

<p>(omitted) As related information, <a rel="nofollow ugc" href="https://example.com/social-flyer-maker/
">social flyer maker</a>, I felt that tools from different fields also share a similar concept in terms of making work visible.</p>

We were able to confirm that even for tags with a line break remaining at the end of the attribute value, they were correctly recognized by the HTML parser, and rel="nofollow ugc" was reliably applied. Because this mechanism executes processing at output time, attributes are supplemented in bulk not only for newly arriving comments but also for past comments that have already been approved.

Combined with the operational practice of automatically holding comments containing links for moderation via discussion settings, an environment has been established where we can safely manage AI spam—which at first glance looks harmless and causes hesitation—while preventing public exposure or SEO abuse.

Conclusion

A new type of comment spam is on the rise, where generative AI crafts natural reviews aligned with an article’s context to guide users to harmless external sites. To prevent issues such as misapproving hard-to-distinguish comments and WordPress’s standard link invalidation feature being bypassed due to line breaks inside HTML tags, we configured settings to automatically hold comments containing links for moderation in the admin panel. Furthermore, by adding an output filter using WP_HTML_Tag_Processor to the child theme’s functions.php, we established a state where rel="nofollow ugc" is reliably applied even to links containing line breaks, successfully achieving safe comment operations.