Word Replacer WordPress Plugin
Word Replacer is a WordPress plugin will enable you to replace a specific word in your content then replace it with replacement given. Contents that can be replaced are posts, pages, or comments. It's identical with the Word Censors in SMF Forum Software.
Readme:
=== Word Replacer ===
Contributors: takien
Donate link: Donate
Tags: replacer, post, comment, page, replace, censor, bbcode, filter
Requires at least: 2.9
Tested up to: 2.9.1
Stable tag: 0.1
Replace word in post, page, or comment.
== Description ==
Word Replacer is a WordPress plugins to replace any desired text/word with your choice. You can filter which content to be replaced, eg. only in page, comment, or post. With very userfriendly administration page you can manage list of word eaasily. It's also can be used to censor any bad/vulgar words in your comment your your guest posting. It's simple but useful.
Feature:
* Userfriendly administration page.
* Define yourself what word to replace in where. (e.g. a word shoud be replaced in comment but not in post etc)
* Uses it's own database table, if you wish you can directly update/add/delete/import through your MySQL admin.
== Installation ==
The installation process.
1. Upload zipped plugin file to the `/wp-content/plugins/` directory. Or Directly upload from your Plugin management page.
2. Activate the plugin through the 'Plugins' menu in WordPress
3. Go to Settings menu and you will see Word Replacer sub menu. Yeah, there are your playground.
== Frequently Asked Questions ==
== Support RegeX ==
No, at this time.
== Screenshots ==
Administration page where you can add/remove your word.
== Changelog ==
= 0.1 =
* First release
The code:
Code
Word Replacer Source Code
<?php
/*
Plugin Name: Word Replacer
Plugin URI: http://wordpress.org/#
Description: Replace word in post, page, or comment
Author: Takien
Version: 0.1
Author URI: http://takien.com/
*/
/* Copyright 2010 takien.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
For a copy of the GNU General Public License, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
global $wpdb;
global $word_replacer;
$word_replacer = Array(
'name' => 'Word Replacer',
'version' => '0.1',
'table_name' => $wpdb->prefix . "word_replacer",
'base_name' => 'wordreplacer'
);
function _specialchar($string) {
return htmlspecialchars(stripcslashes($string));
}
if(isset($_POST['submit'])) {
$id = $_POST['id'];
$original = $_POST['original'];
$replacement = $_POST['replacer'];
$in_posts = $_POST['in_posts'];
$in_comments = $_POST['in_comments'];
$in_pages = $_POST['in_pages'];
$numfield = count($original);
for ($i = 1; $i < = $numfield; $i++) {
if(!empty($original[$i]) && empty($id[$i])) {
$wpdb->query( $wpdb->prepare( "
INSERT INTO ".$word_replacer['table_name']."
(original, replacement, in_posts, in_comments, in_pages)
VALUES (%s, %s, %s, %s, %s)",
array($wpdb->escape($original[$i]), $wpdb->escape($replacement[$i]),$wpdb->escape($in_posts[$i]),$wpdb->escape($in_comments[$i]),$wpdb->escape($in_pages[$i]))));
$message = '
<div id="message" class="updated fade">
<strong>Word Inserted.</strong>
</div>
';
}
elseif(empty($original[$i]) && !empty($id[$i])) {
$wpdb->query("
DELETE FROM ".$word_replacer['table_name']." WHERE id = '".$id[$i]."'");
$message = '
<div id="message" class="updated fade">
<strong>Word Deleted.</strong>
</div>
';
}
elseif(!empty($original[$i]) && !empty($id[$i])) {
$wpdb->update($word_replacer['table_name'],
array('original' => $wpdb->escape($original[$i]),
'replacement' => $wpdb->escape($replacement[$i]),
'in_posts' => $wpdb->escape($in_posts[$i]),
'in_comments' => $wpdb->escape($in_comments[$i]),
'in_pages' => $wpdb->escape($in_pages[$i])
),
array( 'id' => $id[$i] ), array( '%s', '%s', '%s', '%s', '%s'), array( '%d' ) );
$message = '
<div id="message" class="updated fade">
<strong>Word Updated.</strong>
</div>
';
}
}
}
function word_replacer_install () {
global $wpdb;
global $word_replacer;
if($wpdb->get_var('show tables like "'.$word_replacer['table_name'].'"') != $word_replacer['table_name']) {
$sql = "CREATE TABLE " . $word_replacer['table_name'] . " (
id mediumint(9) NOT NULL AUTO_INCREMENT,
original VARCHAR(200) NOT NULL,
replacement VARCHAR(200) NOT NULL,
in_posts VARCHAR(5) NOT NULL,
in_comments VARCHAR(5) NOT NULL,
in_pages VARCHAR(5) NOT NULL,
UNIQUE KEY id (id)
);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
$badword = "badword";
$properword = "goodword";
$insert = "INSERT INTO " . $word_replacer['table_name'] .
" (original, replacement, in_posts, in_comments, in_pages) " .
"VALUES ('" . $wpdb->escape($badword) . "','" . $wpdb->escape($properword) . "','yes','yes','yes')";
$results = $wpdb->query( $insert );
add_option("word_replacer_ver", $word_replacer['version']);
}
}
register_activation_hook(__FILE__,'word_replacer_install');
function word_replacer_db() {
global $wpdb;
global $word_replacer;
return $wpdb->get_results("SELECT * FROM ".$word_replacer['table_name']." ORDER BY id", ARRAY_A);
}
// for content
function word_replacer_postpage($content) {
$i = 1;
foreach(word_replacer_db() as $wrdb) {
$i++;
$original[$i] = stripslashes($wrdb['original']);
$replacement[$i] = stripslashes($wrdb['replacement']);
$in_posts[$i] = stripslashes($wrdb['in_posts']);
$in_comments[$i] = stripslashes($wrdb['in_comments']);
$in_pages[$i] = stripslashes($wrdb['in_pages']);
if(is_page() && ($in_pages[$i] == 'yes')) {
$content = str_replace($original[$i],$replacement[$i],$content);
}
elseif(!is_page() && ($in_posts[$i] == 'yes')) {
$content = str_replace($original[$i],$replacement[$i],$content);
}
}
return $content;
}
// for comment
function word_replacer_comment($content) {
$i = 1;
foreach(word_replacer_db() as $wrdb) {
$i++;
$original[$i] = stripslashes($wrdb['original']);
$replacement[$i] = stripslashes($wrdb['replacement']);
$in_posts[$i] = stripslashes($wrdb['in_posts']);
$in_comments[$i] = stripslashes($wrdb['in_comments']);
$in_pages[$i] = stripslashes($wrdb['in_pages']);
if($in_comments[$i] == 'yes') {
$content = str_replace($original[$i],$replacement[$i],$content);
}
}
return $content;
}
add_filter('comment_text','word_replacer_comment');
add_filter('the_content','word_replacer_postpage');
add_action('admin_menu', 'word_replacer_add_page');
function word_replacer_add_page() {
global $word_replacer;
add_options_page($word_replacer ['name'], $word_replacer ['name'], 'administrator', 'wordreplacer', 'word_replacer_page');
}
function word_replacer_page() {
global $wpdb, $word_replacer, $message;
echo '
<div class="wrap">
<h2>'.$word_replacer ['name'].'</h2>
';
echo $message;
?>
<script language="Javascript" type="text/javascript">
<!--
function addField(area,field,limit) {
if(!document.getElementById) return;
var field_area = document.getElementById(area);
var all_inputs = field_area.getElementsByTagName("input");
var last_item = all_inputs.length - 1;
var last = all_inputs[last_item].id;
var count = Number(last.split("_")[1]) + 1;
if(count > limit && limit > 0) return;
field_area.innerHTML += "
<tr>
<td>
<input type='hidden' name='id[]' value='' />
<input style='width:100%' name='original[]' type='text' /></td>
<td> => </td>
<td>
<input style='width:100%' name='replacer[]' type='text' /></td>
<td>
<input value='yes' name='in_posts[]' type='checkbox' /></td>
<td>
<input value='yes' name='in_comments[]' type='checkbox' /></td>
<td>
<input value='yes' name='in_pages[]' type='checkbox' /></td>
</tr>
";
}
//-->
</script>
<?php
$action_url = $_SERVER[PHP_SELF] . '?page=' . $word_replacer['base_name'];
?>
Put the word to be replaced on the left, and what to change it to on the right.
<form method="post" action="<?php echo $action_url;?>">
<table class="widefat fixed" width="650" align="center" id="word-replacer-list" width="100%">
<thead>
<?php wp_nonce_field('update-options'); ?>
<tr>
<th>Original</th>
<th width="20"> </th>
<th>Replacement</th>
<th width="80">Posts</th>
<th width="80">Comments</th>
<th width="80">Pages</th>
</tr>
</thead>
<?php
$i = -1;
foreach(word_replacer_db() as $wrdb) { $i++ ?>
<?php $alternate = (empty($alternate) ? 'class="alternate"' : '');?>
<tr <?php echo $alternate;?>>
<td>
<input type="hidden" name="id[]" value="<?php echo $wrdb['id']; ?/>" />
<input style="width:100%" type="text" name="original[<?php echo $i;?/>]" id="original_<?php echo $i;?>" value="<?php echo _specialchar($wrdb['original']) ?>" /></td>
<td> => </td>
<td>
<input style="width:100%" type="text" name="replacer[<?php echo $i;?/>]" value="<?php echo _specialchar($wrdb['replacement']) ?>" /></td>
<td>
<input value="yes" name="in_posts[<?php echo $i;?/>]" <?php echo (($wrdb['in_posts'] == 'yes') ? 'checked="checked"' : ''); ?> type="checkbox" /></td>
<td>
<input value="yes" name="in_comments[<?php echo $i;?/>]" <?php echo (($wrdb['in_comments'] == 'yes') ? 'checked="checked"' : ''); ?> type="checkbox" /></td>
<td>
<input value="yes" name="in_pages[<?php echo $i;?/>]" <?php echo (($wrdb['in_pages'] == 'yes') ? 'checked="checked"' : ''); ?> type="checkbox" /></td>
</tr>
<?php } ?>
</table>
<input type="button" value="+ Add More Fields" style="cursor:pointer" onclick="addField('word-replacer-list','original_',0);" />
<input type="hidden" name="action" value="update" />
<input name="submit" class="button-primary" type="submit" value="<?php _e('Add/Update Words') ?/>" />
</form>
Instruction:
1. To <b>Add New Word</b> click Add More Fields add your word, replacement and filter, then hit Add/Update Words.
2. To <b>Update</b> a word, just replace/retype in it's place an hit Add/Update Words.
3. To <b>Delete</b> a word, leave blank/clear word in "original" field and hit Add/Update Words.
</div>
<?php
}
?>
<p>?> Feature Wishlist
Here are some feature wish lists for Word Replacer Plugin based on user comments:
- Replace the title
- Replace Case-insensitive
Uploaded to WordPress.org
This Word Replacer Plugin just approved and uploaded to WordPress Plugins Directory
Please see it in action, here.. http://wordpress.org/extend/plugins/word-replacer/Note: This post was posted by WP Sub Post Plugin
Incoming search terms:
- replacer wordpress
- word replacer
- W3 Total Cache Error: Minify error: Unterminated String:
- wordpress blogs
- wordpress censor words
- where can you find word replcaement
- wordpress replace words
- wordpress plugin replace words in title
- wpdb->escape in wordpress plugin
- wordpress filter bad words post
See Also
Tags: comment replace, content replace, Plugins, str_replace, text replace, word replace, Wordpress, wordpress plugins


Word Replacer WordPress Plugin
wah keren bang plugin nya...
ijin nyimak
mantab gan...
di wp.org dah diaplot belum nih?
udah bro, barusan aja selesai commit ehehe. (lihat sub post diatas)
Very good job. Works great!
One suggestion: perhaps providing a checkbox to make the "badword" not case sensitive. As it is right now, "badword" is filtered and changed to "goodword", but "bAdword" is not. Thanks!
Thanks Sean,
added to Feature Wishlist
I like the layout of your blog and I'm going to do the same thing for mine. Do you have any tips? Please PM ME on yahoo @ AmandaLovesYou702
hi, Charleen Knoepke
it's quite easy i think,
I just saved this page from php.net website,
added some php code on it and uploaded as my wordpress theme...
Great plugin!
I really hope to see the new version with word replace fot Title and case insentivie!! I really need it!!!
Thanks Gale, but I am very busy right now, may be in the next couple of days.
1. Could we remove the first line: "badword => goodword". The heading already tell us how to use it.
2. When adding more fields, could we not reset the form. Just a suggestion, use the html create node and append child (can't remember the exact name) functions to dynamically create the input fields and check-boxes when needed.
Thanks kate for using this plugins
1. Actually you can only remove it from database (e.g using phpMyadmin etc), but unfortunately this plugin require an initial value, otherwise it will return error.... yeah.. this is a bug at this version...
2. No, adding a field will not reset the form... field are added on the fly using JavaScript.
Hi, I recently started a bloghosting platform (based on wordpress MU) and when I stumbled your blog I paid attention to your theme (looking good) so I was wondering can you tell me is it custom made theme or one of those free ones? thanks in advance! regards, blogiskewl
Thanks bloiskewl,

It's a custom made theme.. This is a design of php.net website, I saved it and plugged into my wordpress theme
someone also has asked this here http://takien.com/587/word-replacer-wordpress-plugin.php/comment-page-1#comment-1925
Any possible to Do with Unicode?
When I try with this plugin, it show ??? only.
Thanks
Hello Hikari, I'm sorry currently I don't use Unicode, but I will learn more about this.
Thank you.
That has to do with your database.
I had the same issue.
Log into PHPMyAdmin and change the "Collation" to "utf8_general_ci"
your webhost's default Collation is set to something other than the preferred default "utf8_general_ci" my american webhost's default was set to.. latin1_SWEDISH_ci
yes really... like WTF... anyway, do the changes and you should be fine. I have no more issues.
hahaha, thanks phil
hi
i find a bug in you nice Plugin , if i test your Plugin offline it is ok but if I use it on my webspace than will be follow think happen I write and after update i got this
always if i use this " than i got this \" can you please explain how can i fix this??
thank you.
Sorry so sorry, don't really know about your question, please give me detailed info.
However I have tested it in several server and works fine both when replacing single quote (') and double quotes (").
Thank you
ok wenn i use a adress like *ttp":*** than got *ttp:\***
always one \ if i update again than :\\** and so on after 10 words :\\\\\\\\\\***
I dont know why , i just make a clean install i you like to test send me a mail you got the date for the test acount... for testing
Thank you
Okay, check your mail inbox, thanks.
Hi the_beginner,
It seems `magic_quotes_gpc` in your php settings is ON
you should set it OFF to fix this problem.
It's now fixed
just change two lines
before
<?php
if(isset($_POST['submit'])) {
$id = $_POST['id'];
$original = $_POST['original'];
$replacement = $_POST['replacer'];?>
after:
<?php
if(isset($_POST['submit'])) {
$id = $_POST['id'];
$original = stripslashes_deep($_POST['original']);
$replacement = stripslashes_deep($_POST['replacer']);?>
many thanks to you the_beginner, for reporting this bugs.
I have replaced the two lines but the programm crashes. I am a complete php noob. Could someone send me the working version.
Thank you.
Anton
Thanks, anton.
please reinstall your plugins, and edit the code carefully.
to All:
I'm sorry it's too late to reply at this post because of my business.
@takien thank you for the fix now it is working here on my server

Important for using Wordreplacer > here
If you have a word "test" <a href....
and you have like this in your text (html code) "Get the latest Flash Player" than you get this Get the 'la "here is now your link" Flash Player'
But if you write in your database " test " with a space befor and after the word than works good
You're right the_beginner,
by default, str_replace() function will replace all occurences in the string given, even if it's only a part of word.
However I am currently working on this plugin for next release, I use preg_replace() rather than str_replace() because preg_replace() is more flexible.
The preview of next relase is: Regex support, case insensitive, replace title, and of course as you said "search whole word only".
I worked hard last night dealing with Database upgrade process in this plugin. Hahaha.. It was confusing for me because this is my first plugin that using sparate mysql table.... and finally it's done.
Thank you for your comment,
all other idea/suggestion/feature request will be much appreciated.
Regards,
Hi takien, for some reason i cant get it to work on my blog:
DjSmuggla.com
Whenever i put a badword in the left box & a goodword in the right, & click Add/UpDate Words nothing happens.
The word doesn't get replaced...
Do you know why m8?
I really want to use this aswell...
Thanks DjSmuggla, what do you mean with The word doesn't get replaced...
you have to check which one to be replaced, Posts, Comments or Pages
Will you release the next version soon?
hello taiken,
has "replacing titles" been implemented ?
I'm waiting for this or I can't seem to get it to work.
Thanks,
Phil
I tried to change a common word to change in a word Could it be done? Because I get two backslahses in the http part.
You could solve this problem by read my conversation with the_beginner above.
or simply follow this link http://takien.com/587/word-replacer-wordpress-plugin.php/comment-page-1#comment-2528
Thanks
Hi,
I am a news blogger from Turkiye. I have a problem about your plugin.
My blog's language is Turkish therfore i am using special Turkish characters. This plugin cannot support them.
For example i can replace "Saadet Partisi" with "Saadet Partisi" but i cannot replace "Numan KurtulmuÅŸ" with "Numan KurtulmuÅŸ"
Thank you for your unique plugin.
Regards, Cihad.
Thanks Cihad Sevim,
I'm so sorry, actually I never dealing with Turkish character and other than Latin, but you can still browse to the source code of the plugin, and ask to fix it yourself or ask your friends who's familiar with Turkish character.