CM Search and Replace (CMSR) - Use Case - Example of Regex and Storing Results In the Database


Use Case - Example of Regex and Storing Results in the Database

Back to User Guide

Note: This guide covers features from:

Video Use-Case


Introduction

CM Search and Replace allows you to easily replace text in all types of posts and pages, including theme and plugins generated content. 

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

Creating Regex rule that removes all capital letters:

Result on the front-end:

Committing permanent changes to the database using the rule:

Committing permanent changes to the database using the widget:

Use Case Assumptions

In this example use case guide, we'll consider an example of Regex and how to store results in the database. 

We consider that you have already bought the plugin, but not installed it. 

It follows:


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

If you still have question with these steps:

  • Installing the plugin
  • Creating Replacement Rules
    • Base Configuration
    • Restrictions
      • Timeframe
      • Exclusions
  • Settings
  • Replacement Widget
  • Export/Import 

Then check the guide: CM Search and Replace (CMSR) - Use Case - How to Replace Content On the Fly

Using Regex

A regular expression (abbreviated regex or regexp and sometimes called a regular expression) is a sequence of characters that define a search pattern, mainly to use in pattern matching with strings, or string matching. 

Let's consider a simple example: we need to remove all capital letters from the content. We can use this regular expression for it:  /[A-Z]/

To create a rule, navigate to  Admin Dashboard → Search & Replace → Settings.

Under the  Replacement Rules tab find a dashboard for creating rules at the bottom of the page.

To create a Regex rule, do the following:

  1. Put /[A-Z]/ to the From String area.
  2. Keep empty the area To String.
  3. Make sure to enable the checkbox Regex.
  4. Choose the Location where to commit the rule (optionally)
  5. Click Add New Rule to create a rule.

Now let's check the result on the front-end. To showcase this example clearer, we will use the Replacement Widget that allows enabling or disabling replacements on the Front-end.

As you can see - all capital letters are removed.

TIP: Learn More About Regex

Some examples of Regex resources:

You can use Regex in the search and replace rules to create more specific and defined replace rules.

Saving Permanent Changes to the Database

By default, the plugin  does not make permanent changes to your content. Once you create replacement rules, they will be applied to visitors only while they access the page.

There are two ways how to commit permanent changes to content:

  • Using the replacement rule
  • Using the replacement widget.

Saving Permanent Changes Using the Rule

To commit permanent changes using the rule, you need to click the button UpdateDb for the relevant rule. Be careful, as this method commits permanent changes to the database for all posts that are included in the Location field (or for all except the excluded ones).

When you click the button UpdateDb a pop-up message will appear with a warning. If you are sure about it, click Ok.

As a result, the post on the back-end is changed according to the rule:

Saving Permanent Changes Using the Widget

Another way for committing changes is using the Replacement Widget. To make it work, head to the Replacement Widget tab.

Choose the option Show to admin only in the Display Widget for dropdown. One more option will appear - Admin can save changes to database. You need to enable it too.

Don't forget to click the button Save Changes at the bottom of the page.

As a result, the admin will see the button Make changes permanent in DB on the posts with replacements.

This widget commits permanent changes only to the specific post where it used. Be careful, as there is no warning message when you click on it.

Let's see how it works:

End Result

Following instructions found in the plugin and guides, you should be able to create Regex rules and commit permanent changes to the database.

Use Case Front-End

Creating Regex rule that removes all capital letters:

Result on the front-end:

Committing permanent changes to the database using the rule:

Committing permanent changes to the database using the widget:


More information about the Search and Replace WordPress Plugin

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
Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.