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:
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
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…