<?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>?>