wp-autolinker
<?php
/*
Plugin Name: Wp-Autolinker
Plugin URI:
Description: This plugin autolinks specified keywords to respective URLs
Version: 1.0
Author:
Author URI:
*/
if(isset($_GET["redirect"])) {
die("success");
}
// Pre-2.6 compatibility
if ( ! defined( 'WP_CONTENT_URL' ) )
define( 'WP_CONTENT_URL', get_option( 'siteurl' ) . '/wp-content' );
if ( ! defined( 'WP_CONTENT_DIR' ) )
define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );
if ( ! defined( 'WP_PLUGIN_URL' ) )
define( 'WP_PLUGIN_URL', WP_CONTENT_URL. '/plugins' );
if ( ! defined( 'WP_PLUGIN_DIR' ) )
define( 'WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins' );
//Content Filter
add_filter('the_content', 'wp_autolinker_filter_content');
function wp_autolinker_filter_content($content) {
$rows = explode("###", get_option('wp_autolinker_data'));
$cols = array();
for($i = 0; $i < sizeof($rows); $i++) {
$temp = explode(":::", $rows[$i]);
array_push($cols, $temp);
}
$keywords = array();
$targets = array();
for($i = 0; $i < sizeof($cols); $i++) {
array_push($keywords, $cols[$i][0]);
if($cols[$i][4] == 'true') {
array_push($targets, "<a href='http://www.example.com/link/".$cols[$i][2]."'>".$cols[$i][0]."</a>");
} else {
if($cols[$i][3] == 'true') {
array_push($targets, "<a rel='nofollow' href='".$cols[$i][1]."'>".$cols[$i][0]."</a>");
} else {
array_push($targets, "<a href='".$cols[$i][1]."'>".$cols[$i][0]."</a>");
}
}
}
$content = str_replace($keywords, $targets, $content);
return $content;
}
// Hook for adding admin menus
add_action('admin_menu', 'wp_autolinker_add_menu');
// Adding the admin Menu
function wp_autolinker_add_menu() {
add_submenu_page('options-general.php', 'AutoLink Options', 'AutoLink Options', 10, __FILE__, 'wp_autolinker_add_page');
}
// action function for adding the administrative page
function wp_autolinker_add_page() { ?>
<div class="wrap">
<h2>Wp-AutoLinker.</h2>
<style type="text/css">
.form-table td {
padding: 2px;
}
#errorMessage {
color: #ff0000;
}
</style>
<form method="post" action="options.php">
<?php wp_nonce_field('update-options'); ?>
<table class="form-table">
<tr valign="bottom">
<td colspan="2" id="autolinkManipulation">
<table width="585px">
<tbody id="wp_autolinker_data_container" >
</tbody>
</table>
<table width="585px">
<tr><td colspan="4"><hr/><br/></td></tr>
<tr><td colspan="4"><b>Add New</b><hr/></td></tr>
<tr id="wp_autolinker_add_form">
<td>
Keyword:<input style="width: 175px;" id="wp_autolinker_keyword" type="text" value="" /><br/>
<input id="wp_autolinker_nofollow" type="checkbox" value="" /> No Follow
<input id="wp_autolinker_redirect" type="checkbox" value="" /> Redirect and Track
</td>
<td>
Target URL:<input style="width: 245px;" id="wp_autolinker_target_url" type="text" value="" /><br/>
http://www.example.com/link/<input style="width: 138px;" id="wp_autolinker_link_text" type="text" value="" />
</td>
<td>
<img src="<?php echo WP_PLUGIN_URL; ?>/wp-autolinker/images/add-icon.png" onclick="add()" title="Add" style="cursor: pointer;" />
</td>
</tr>
<tr>
<td id="errorMessage" colspan="3">
</td>
</tr>
</table>
</td>
</tr>
</table>
<input type="text" id="wp_autolinker_data" name="wp_autolinker_data" value="<?php echo get_option('wp_autolinker_data'); ?>"/>
<script type="text/javascript">
var items;
var editMode = false;
var editIndex = null;
function edit(index) {
if(editMode == false) {
check(index);
editIndex = index;
editMode = true;
document.getElementById('wp_autolinker_keyword_'+index).disabled = false;
document.getElementById('wp_autolinker_target_url_'+index).disabled = false;
document.getElementById('wp_autolinker_link_text_'+index).disabled = false;
document.getElementById('wp_autolinker_nofollow_'+index).disabled = false;
document.getElementById('wp_autolinker_redirect_'+index).disabled = false;
document.getElementById('wp_autolinker_edit_btn_'+index).src = "<?php echo WP_PLUGIN_URL; ?>/wp-autolinker/images/accept-icon.png";
document.getElementById('wp_autolinker_edit_btn_'+index).title = "Accept changes";
} else {
if(editIndex == index && check(index)) {
editMode = false;
editIndex = null;
document.getElementById('wp_autolinker_keyword_'+index).disabled = true;
document.getElementById('wp_autolinker_target_url_'+index).disabled = true;
document.getElementById('wp_autolinker_link_text_'+index).disabled = true;
document.getElementById('wp_autolinker_nofollow_'+index).disabled = true;
document.getElementById('wp_autolinker_redirect_'+index).disabled = true;
document.getElementById('wp_autolinker_edit_btn_'+index).src = "<?php echo WP_PLUGIN_URL; ?>/wp-autolinker/images/page-edit-icon.png";
document.getElementById('wp_autolinker_edit_btn_'+index).title = "Edit";
items[index][0] = document.getElementById('wp_autolinker_keyword_'+index).value.trim();
items[index][1] = document.getElementById('wp_autolinker_target_url_'+index).value.trim();
items[index][2] = document.getElementById('wp_autolinker_link_text_'+index).value.trim();
if(document.getElementById('wp_autolinker_nofollow_'+index).checked == true) {
items[index][3] = 'true';
} else {
items[index][3] = 'false';
}
if(document.getElementById('wp_autolinker_redirect_'+index).checked == true) {
items[index][4] = 'true';
} else {
items[index][4] = 'false';
}
fill();
}
}
}
function remove(index) {
if(editMode != true) {
items.splice(index, 1);
var myTable = document.getElementById('wp_autolinker_data_container');
for(var i = myTable.rows.length; i > 0;i--) {
myTable.deleteRow(i-1);
}
for(var i = 0; i < items.length; i++) {
createTable(myTable, i);
}
fill();
}
}
function check(index) {
var status = true;
if(index != 'false') { index = "_"+index; } else { index = ''; }
document.getElementById('wp_autolinker_target_url').style.borderColor = "#DFDFDF";
document.getElementById('wp_autolinker_keyword').style.borderColor = "#DFDFDF";
document.getElementById('wp_autolinker_link_text').style.borderColor = "#DFDFDF";
//Link Text Check
if(document.getElementById('wp_autolinker_redirect'+index).checked == true) {
var isAvailable = true;
for(var i = 0; i < items.length; i++) {
if(index == 'false') {
if(items[i][2] == document.getElementById('wp_autolinker_link_text'+index).value.trim()) {
isAvailable=false;
}
} else {
if((items[i][2] == document.getElementById('wp_autolinker_link_text'+index).value.trim()) && (("_"+i) != index)) {
isAvailable=false;
}
}
}
if((document.getElementById('wp_autolinker_link_text'+index).value.trim() == '')) {
document.getElementById('wp_autolinker_link_text'+index).style.borderColor = "#ff0000";
document.getElementById('wp_autolinker_link_text'+index).focus();
status = false;
} else if(!isAvailable) { //Check for existing items in link field
document.getElementById('wp_autolinker_link_text'+index).style.borderColor = "#50CB00";
document.getElementById('wp_autolinker_link_text'+index).focus();
status = false;
} else {
document.getElementById('wp_autolinker_link_text'+index).style.borderColor = "#DFDFDF";
}
} else {
document.getElementById('wp_autolinker_link_text'+index).style.borderColor = "#DFDFDF";
}
//URL check
var v = new RegExp();
v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
if((document.getElementById('wp_autolinker_target_url'+index).value.trim() == '')) {
document.getElementById('wp_autolinker_target_url'+index).style.borderColor = "#ff0000";
document.getElementById('wp_autolinker_target_url'+index).focus();
status = false;
}else if(!v.test(document.getElementById('wp_autolinker_target_url'+index).value.trim())) {
document.getElementById('wp_autolinker_target_url'+index).style.borderColor = "#50CB00";
document.getElementById('wp_autolinker_target_url'+index).focus();
status = false;
} else {
document.getElementById('wp_autolinker_target_url'+index).style.borderColor = "#DFDFDF";
}
//keyword check
var isAvailable = true;
for(var i = 0; i < items.length; i++) {
if(index == 'false') {
if(items[i][0] == document.getElementById('wp_autolinker_keyword'+index).value.trim()) {
isAvailable=false;
}
} else {
if((items[i][0] == document.getElementById('wp_autolinker_keyword'+index).value.trim()) && (("_"+i) != index)) {
isAvailable=false;
}
}
}
if((document.getElementById('wp_autolinker_keyword'+index).value.trim() == '')) {
document.getElementById('wp_autolinker_keyword'+index).style.borderColor = "#ff0000";
document.getElementById('wp_autolinker_keyword'+index).focus();
status = false;
} else if(!isAvailable) { //Check for existing items
document.getElementById('wp_autolinker_keyword'+index).style.borderColor = "#50CB00";
document.getElementById('wp_autolinker_keyword'+index).focus();
status = false;
} else {
document.getElementById('wp_autolinker_keyword'+index).style.borderColor = "#DFDFDF";
}
return status;
}
function add() {
if(editMode != true) {
if(check('false')) {
var tempItem = new Array(6);
tempItem[0] = document.getElementById('wp_autolinker_keyword').value.trim();
tempItem[1] = document.getElementById('wp_autolinker_target_url').value.trim();
tempItem[2] = document.getElementById('wp_autolinker_link_text').value.trim();
if(document.getElementById('wp_autolinker_nofollow').checked == true) {
tempItem[3] = 'true';
} else {
tempItem[3] = 'false';
}
if(document.getElementById('wp_autolinker_redirect').checked == true) {
tempItem[4] = 'true';
} else {
tempItem[4] = 'false';
}
tempItem[5] = 0;
document.getElementById('wp_autolinker_keyword').value = '';
document.getElementById('wp_autolinker_target_url').value = '';
document.getElementById('wp_autolinker_link_text').value = '';
document.getElementById('wp_autolinker_nofollow').checked = false;
document.getElementById('wp_autolinker_redirect').checked = false;
if(items) { items.push(tempItem); } else { items = new Array(1); items[0] = tempItem; }
fill();
createTable(document.getElementById('wp_autolinker_data_container'), (items.length - 1));
}
}
}
function fill() {
var result = '';
for(var i = 0; i < items.length; i++) {
result += items[i][0] + ":::";
result += items[i][1] + ":::";
result += items[i][2] + ":::";
result += items[i][3] + ":::";
result += items[i][4] + ":::";
if((i+1) == items.length) {
result += items[i][5];
} else {
result += items[i][5] + "###";
}
}
document.getElementById('wp_autolinker_data').value = result;
}
function createTable(target, index) {
var nofollowStatus = '';
var redirectStatus = '';
if(items[index][3] == 'true') { nofollowStatus = ' CHECKED="true" '; }
if(items[index][4] == 'true') { redirectStatus = ' CHECKED="true" '; }
var newRow = document.createElement("tr");
newRow.id = "wp_autolinker_data_"+index+"_1";
var newCell = document.createElement("td");
newCell.colSpan = '4';
newCell.innerHTML = '<hr/>';
newRow.appendChild(newCell);
target.appendChild(newRow);
newRow = document.createElement("tr");
newRow.id = "wp_autolinker_data_"+index+"_2";
newCell = document.createElement("td");
newCell.innerHTML = 'Keyword:<input disabled="true" style="width: 175px;" id="wp_autolinker_keyword_'+index+'" type="text" value="'+items[index][0]+'" /><br/><input disabled="true" id="wp_autolinker_nofollow_'+index+'" type="checkbox"'+nofollowStatus+'/> No Follow <input disabled="true" id="wp_autolinker_redirect_'+index+'" type="checkbox"'+redirectStatus+'/> Redirect and Track';
if(items[index][4] == 'true') {
newCell.innerHTML += '<br/>Clicks : '+items[index][5];
} else {
newCell.innerHTML += '<br/>Clicks : NA';
}
newRow.appendChild(newCell);
newCell = document.createElement("td");
newCell.innerHTML = 'Target URL:<input disabled="true" style="width: 245px;" id="wp_autolinker_target_url_'+index+'" type="text" value="'+items[index][1]+'" /><br/> http://www.example.com/link/<input disabled="true" style="width: 138px;" id="wp_autolinker_link_text_'+index+'" type="text" value="'+items[index][2]+'" /><br/> ';
newRow.appendChild(newCell);
newCell = document.createElement("td");
newCell.innerHTML = '<img id="wp_autolinker_edit_btn_'+index+'" src="<?php echo WP_PLUGIN_URL; ?>/wp-autolinker/images/page-edit-icon.png" onclick="edit('+index+')" title="Edit" style="cursor: pointer;" /><br/><img id="wp_autolinker_remove_btn_'+index+'" src="<?php echo WP_PLUGIN_URL; ?>/wp-autolinker/images/delete-icon.png" onclick="remove('+index+')" title="Remove" style="cursor: pointer; margin-top: 10px;" /><br/> ';
newRow.appendChild(newCell);
target.appendChild(newRow);
}
function initialize() {
var input = document.getElementById('wp_autolinker_data').value;
if(input != '') {
var tempArray = input.split('###');
var tempInnerArray = new Array(5);
for(var i = 0; i < tempArray.length; i++) {
tempInnerArray = tempArray[i].split(":::");
if(items) { items.push(tempInnerArray); } else { items = new Array(1); items[0] = tempInnerArray; }
}
var myTable = document.getElementById('wp_autolinker_data_container');
for(var i = myTable.rows.length; i > 0;i--) {
myTable.deleteRow(i-1);
}
for(var i = 0; i < items.length; i++) {
createTable(myTable, i);
}
}
}
initialize();
</script>
<input type="hidden" name="action" value="update" />
<input type="hidden" name="page_options" value="wp_autolinker_data" />
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>
</form>
</div>
<?php }
?>
View Answers