CM Search and Replace (CMSR) - Use Case - How to Replace Content On the Fly
Use Case - How to Replace Content On the Fly
Note: This guide covers features from:
- CM Search and Replace Pro - This document uses version 1.4.2.
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, every time a visitor visits a page with "x", he/she will see "y".
Note that the content will only change to the visitor. The change will not be applied to your database.
Use Case Front-End
Replacing "nancy" to "REGINA" and using Replacement widget:
Use Case Assumptions
In this example use case guide, we'll cover how to create rules for replacing the content on the fly.
We consider that you have already bought the plugin, but not installed it.
- Installing the plugin
- Creating Replacement Rules
- Replacement Widget
- End Result
- Extra - Export/Import
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
Creating Replacement Rules
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.
To create a rule, navigate to Admin Dashboard → Search & Replace → Settings.
Under the Replacement Rules tab you can find a dashboard for creating rules.
To create a rule, we need to use the form at the bottom of the page:
Let's start with some simple rule. For instance, we have a post with a book about Nancy Drew. And we need to replace the name Nancy to another one - for example, REGINA. So, we put a word/phrase we need to replace into the text area From String. Then we put a word/phrase that will be instead of this to the text area To String.
Then we need to choose a place where the search and replace will be committed. There are 3 options under the Location dropdown:
- All - To commit this rule of searching and replacing everywhere on the site.
- Include - To commit this rule only on the chosen posts/pages.
- Exclude - To commit this rule on all posts/pages, except the chosen ones.
As now we need to commit this rule on one definite post, we choose the option Include and add needed post to the list. You can add any number of posts and pages to these lists.
Then click the button Add New Rule.
Our first rule is created! Now let's see the result on the Front-end:
Note: In this front-end example we used the widget to enable and disable replacements - we will talk about it more detailed lower.
As you could see, in this example the rule wasn't case-sensitive - we used the word "nancy" for replacing, and all "Nancy" words were replaced. Now let's create a case-sensitive rule. We do all the same, but also turn on the option Case, as shown on the screenshot below.
Now let's see the result on the Front-end:
As you can see, the words with exact match were replaced.
Managing Replacement Rules
There are a few actions you can do to manage your rules:
- Pause - You can stop committing any rule at any moment by just checking the box Pause and clicking Update. Uncheck the box to make this rule work again.
- Update - Click this button to save changes every time you edit the rule.
- UpdateDb - The main functionality of the plugin is making replacements on the fly, without changing them to the database. However, it's possible to commit replacement rules to the database by clicking this button. Learn more about it in this guide: CM Search and Replace (CMSR) - Saving Changes to the Database
- Delete - Click this button to remove the rule.
- Save All Changes - If you edited a few rules, you can update them all at once by clicking this button.
There are additional options you can set for each rule - these are Timeframes and Exclusions.
Setting a timeframe means that this rule will be applied only within the range of dates and time defined in the restriction. You can add more than one timeframe.
To add timeframe click on the button Add Timeframe.
A new box will appear for setting From and To dates and time for the restriction.
Important: when you set the date and time for the restriction, you need to know that this option works according to the Universal time - and it can be different from the local time that you set in your site settings. To check it, navigate to Admin Dashboard → Settings → General and scroll to the option Timezone.
There are displayed Universal time and Local time.
For example, the case that you see on the screen we have a difference in 3 hours between universal and local time set on the site. So, if we want the replacement rule work from 12 to 13 o'clock according to the local time, we need to set the time in the Add Timeframe option from 9 to 10 o'clock.
Adding exclusions means that this rule will not be applied to the chosen items like:
- Site Title
- Page Title
To add exclusion click on the button Add Exclusion.
A new box will appear with a set of checkboxes.
Each of the checkboxes allows excluding given rule from working in associated area. For example, checking Page Title means that this rule will not work in titles, even if it's defined differently in the General Settings.
Under the Settings tab you can define some general settings for all rules.
- Search & Replace edit page display - Choose if to display items on a single page or paginate them.
- Search & Replace items per page - Define amount of items per page. Works only if you've chosen to paginate items in the previous option.
- Search & Replace on post types - Enable search and replace for posts, pages or any other custom post types.
Search & Replace excluded posts/pages - Set here posts and pages that you want to exclude from search and replace parsing.
Note: options Search & Replace on post types and Search & Replace excluded posts/pages have higher priority than the options that you define in rules. For instance, if in the rule you include some post to the rule for parsing, but this post is excluded in the Search & Replace excluded posts/pages option, then the rule will not be committed for that post.
Last 5 options have lower priority than corresponding options in creating rules. So, they can be redefined for each rule:
- Search & Replace in the site title
- Search & Replace in the page/post title
- Search & Replace in content
- Search & Replace in excerpt
- Search & Replace in comments
The plugin has a Replacement Widget that allows to control on the Front-end if to enable or disable replacement. You could see how it works in the example above.
To configure the widget head to the Replacement Widget tab.
The options are:
- Display Widget for - Select who can see the Widget:
- Show to anyone - In this case the widget will be shown to all users, including non-logged-in.
- Show to logged users - The widget will be shown only to logged-in users.
- Show to admin only - The widget will be shown only to admin.
- Admin can save changes to database - This option is available only if you've chosen Show to admin only in the previous option. In this case, the admin can commit permanent changes to the database for the post, where he uses this option.
- Tooltip ON/OFF Widget Label - Here you can customize or translate the Widgets Label.
- Tooltip ON/OFF Widget disable text - Customize the text of the link disabling the Search & Replace functionality.
- Tooltip ON/OFF Widget enable text - Customize the text of the link enabling the Search & Replace functionality.
- Add to the top/bottom of each post - Note that it will only be added to the post types selected in the general setting.
How it looks like on the Front-end:
- Tooltip ON/OFF Widget Label.
- Tooltip ON/OFF Widget disable/enable text.
- Admin can save changes to database (this button can be displayed only for admins).
Following instructions found in the plugin and guides, you should be able to configure replacing content on the fly.
Use Case Front-End
Replacing "nancy" to "REGINA" and using Replacement widget:
Extra - Export/Import
CM Search and Replace allows you to export and import all replacement rules, including paused ones. This is done via a .json file.
This is useful for transferring rules from different sites or add/remove them in batch.
Navigate to Admin Dashboard → Search & Replace → Export/Import.
There you can see two sections - Export and Import.
There are two ways of exporting:
- Export all your replacement rules (even paused) and plugin settings.
- Export only replacement rules, without plugin settings. To do this you need to check the box Export replacement rules only (.CSV format).
To export the file, click the button Download Export File.
To import replacement rules (and plugin settings if you've exported them), click the button Choose File to upload the file. Then click the button Upload file and import.