Search and Replace Plugin (CMSR) - Use Case - How to Filter and Remove Inappropriate Content from Comments
Use Case - How to Filter and Remove Inappropriate Content from Comments
Note: This guide covers features from:
- Search and Replace plugin - This document uses version 1.6.8.
Introduction
The Search and Replace plugin enables you to replace text across posts, pages, and content generated by themes or plugins.
It works via rules: you tell it "x" should become "y". Then, everytime a visitor visits a page with "x", he/she will see "y".
Note that the content will only change to the visitor. By default the change will not be applied to your database.
Use Case Front-End
Removing/replacing a spam link in the comment:
Removing/replacing rude words in the comment:
Use Case Assumptions
In this example use case guide, we'll consider how to remove unwanted content from comments on your site, such as spam links and rude words. You can apply the same idea for removing or replacing other types of unwanted content from comments.
We consider that you have already bought the plugin, but not installed it.
It follows:
- Installing the plugin
- Quick recap
- Using regex
- Creating search and replace rules
- Removing spam links
- Removing rude words
- End result
Installing the Plugin
The process is the same for all CM plugins.
- Download the plugin from your customer dashboard.
- Log in to WordPress and navigate to the WordPress Admin → Plugins settings.
- Click on Add New.
- Activate it and add the license.
Learn more: Getting Started - Plugin Overview
Quick Recap
In this use case guide, we will focus specifically on removing unwanted content from comments. If you want to learn more about the plugin and its features, you can check out the more general use case guide:
Using Regex
Regular expressions (regex) are powerful patterns used to match and manipulate specific text. By defining clear patterns, regex allows precise filtering and ensures that only the intended text is modified or removed. This makes it an essential tool for automating content moderation efficiently.
Regex is ideal for tasks like detecting and removing links or other unwanted content since it allows you to define a single pattern rather than creating separate rules for each instance. So, if you want to remove or replace links, you just use that one pattern, and it will catch all the links at once. The same idea applies to other things, like unwanted tags or bad words.
Creating Search and Replace Rules
Now let's go through the basic process of creating search and replace rules and consider a few options we will need in this use case guide.
To create search and replace rules in the Search and Replace plugin, navigate to Admin Dashboard → Search & Replace → Settings → Replacement Rules tab.
At the top of the tab you can find the search field and two checkboxes "For" and "By". It will help you to search and navigate between multiple replacement rules that you can create.
Below the search bar there's a button Add New S&R Rule which shows or hides a form for creating rules:
The form by default shows only basic options. More options can be shown or hidden by clicking Expand / Collapse Additional Settings:
The options we will need in this use case are:
- Search For - Enter here the content you want to replace. That's the field where we will place our regular expressions.
- Replace By - Enter here the content that you want to be displayed instead of the searched content. If you leave this field empty, the content from the Search For field will not be replaced, but just removed.
- Regex - Enabling this option is required if you want the content placed to the Search For field to be parsed as a regex rule.
- Exclusions - This option allows to not apply the rule to the associated area. For our use case this option is useful as we will be able to exclude from searching and replacing all areas except the comments section.
Note
To parse content in the comments section of your site, you need to make sure that the relevant option is enabled in the plugin settings. To do that, head to the Settings tab and enable the option Post/Page Comments in case if it's not enabled yet, then save the changes.
The form for creating search and replace rules has more options than we considered. If you want to learn more about all of them and how to manage all your rules, please check this user guide:
Now let's move on to creating rules for removing unwanted content.
Removing Spam Links
First case we are going to consider is removing spam links.
Let's say that someone has left a comment that contains a link to the competitor site:
To avoid appearing unwanted links in comments, we will use the following regex:
- /<a\b[^>]*>(.*?)<\/a>/
This rule detects all content wrapped with the <a> tag which displays a link.
So let's create a rule. We configure the rule in the following way:
- Search For - We add here our regular expression.
- Replace By - We type here text that will be displayed instead of the link.
- Regex - Enable this option.
- Exclusions - Here we exclude all items except Links and Comments.
Then click the button Add Rule.
Now let's see the result on the front-end - the link is replaced by text we defined:
Removing Rude Words
Second case we are going to consider is removing rude words.
For example, we have some users that leave comments with lots of rude words:
To avoid such kind of language in comments, we will use the following regex:
- \b(word1|word2|word3)\b
This regex allows to detect multiple specific words.
So let's create a rule. We configure the rule in the following way:
- Search For - Using regex we define words which we want to remove or replace.
- Replace By - We type here text that will be displayed instead of the found word.
- Regex - Enable this option.
- Exclusions - Here we exclude all items except Comments.
Then click the button Add Rule.
Now let's see the result on the front-end - the all specified rude words are replaced by another content:
End Result
Following instructions found in the Search and Replace plugin and guides, you should be able to remove unwanted content from comments on your site, such as spam links and rude words.
Use Case Front-End
Removing/replacing a spam link in the comment:
Removing/replacing rude words in the comment:
More information about the Search and Replace Plugin for WordPress Other WordPress products can be found at CreativeMinds WordPress Store |
|
Let us know how we can Improve this Product Documentation Page To open a Support Ticket visit our support center |