Hijack Comment Links: A WordPress Plugin

I joined Wicked Fire a few days ago where i caught a post by Matt3 about hijacking links in your WordPress blogs.

Basically, he said that some of the scripts he had been using were scraping comments, but leaving the comments links intact, which is generally not very helpful.

This Plugin will take a list of links (one per line), search through comments, for http:// style strings and replace them with your links.

It will also remove “nofollow” from links if you ask it to.

By default it works only on new posts, but it can work retroactively on old posts as well. If you use the retro active mode then there is no guarantee that the links won’t change there targets when you update your list of URLs. Chances are that 75% of the time, you will not want to use the retro active link injection feature.

Download Hijack Comment Links

Install it like any other wordpress plugin. I.e. unzip it to ./wp-contents/plugins/ and active from wp-admin.

No i don’t use this script on this site, but i do ‘follow’ your comments so why don’t you take a second and leave a comment.

Hijack Comment Links Code

For those who like to look over the code.

zzz-hijack-comment-links.php

 
<?php
/*
 
Plugin Name: Hijack Comment Links
Plugin URI: http://dfhu.org/scripts/
Description: Replace Comment Links With Links of Your Choice
Author: Victory
Version: 1.0 (really see source for right Version number)
Author URI: http://dfhu.org/
 
@author: Victory
@site: http://dfhu.org
@copyright: dfhu.org
@report_bugs: bugs(at)dfhu.org
@feature_request: features(at)dfhu.org
@file: zzz-hijack-comment-links/zzz-hijack-comment-links.php
@license: BSD
@version: 5
 
@description:
 
  This Wordpress Plugin, replaces the 'http://.*' style strings in
  comments with random urls of your choice.
 
$Date:: 2009-08-01 15:36:46 #$:
$Rev ::                      $:
 
*/
 
 
function hijack_comment_links_warning() {
  // Post a Little message to the wordpress admin to let people know
  // they have yet to update the list of links they wish to inject.
 
  echo "
<div id='dfhu-warning' class='updated fade'>
<p><strong>"
.__('Hijack Comment Links is Almost Ready.')
."</strong> "
.sprintf(__('You must 
<a href="%1$s">enter the links you want to inject</a> for it to work.'), 
	 "options-general.php?page=hijack-comment-links-group")
."</p></div>
";
}
 
function hijack_comment_links_menu() {
  // Just creat the submenu for the options.
  add_submenu_page('options-general.php',
		   'Hijack Comment Links Options', 
		   'Hijack Comment Links Options', 
		   8, 
		   'hijack-comment-links-group', 
		   'hijack_comment_links_options');   
}
 
// If the admin hasn't put any links in the yet ...
if(get_option('hjcl_links_to_inject') == ""){
  // ... then we suggest that she does so.
  add_action('admin_notices', 
	     'hijack_comment_links_warning');
}
 
// if we are admin ...
if ( is_admin() ){
  // then let us mess around with hjcl's settings
  add_action('admin_menu', 'hijack_comment_links_menu');
  add_action('admin_init', 'hijack_comment_links_settings' );
}
 
function hijack_comment_links_settings(){
  // register the links, nofollow and retro settings
  register_setting('hijack-comment-links-group', 
		   'hjcl_links_to_inject');
  register_setting('hijack-comment-links-group', 
		   'hjcl_remove_nofollow');
  register_setting('hijack-comment-links-group', 
		   'hjcl_retro');
}
 
 
function hijack_comment_links_options() { 
  // The Options Page for HJCL
  ?>
  <h2>Hijack Comment Links Options</h2>
  <form method="post" action="options.php">
    <input type="hidden" name="action" value="update" />
 
    <input 
  type="hidden"
  name="page_options" 
  value="hjcl_links_to_inject,hjcl_remove_nofollow,hjcl_retro"/>
 
    <?php wp_nonce_field('update-options');  
         settings_fields('hijack-comment-links-group' );
    ?>
 
    <table class="form-table">
 
      <tr>
	<th scope="row">
Enter <b>1</b> To Remove nofollow,
<b>0</b> To Keep nofollow.
	</th>
	<td>
	  <input 
	     type="text" 
	     name="hjcl_remove_nofollow" 
	     value="<?php 
echo intval(get_option('hjcl_remove_nofollow')); 
           ?>">
	</td>
      </tr>
 
      <tr>
	<th scope="row">
Enter <b>1</b> To Effect Links Retroactively (not recommended), 
<b>0</b> To just inject on new posts.
	</th>
	<td>
	  <input 
	     type="text" 
	     name="hjcl_retro" 
	     value="<?php 
echo intval(get_option('hjcl_retro')); 
           ?>">
	</td>
      </tr>
 
 
 
      <tr valign="top">
	<th scope="row">
	  <b>List of URLs to Inject. One Per Line.</b> 
	</th>
	<td>
	  <textarea 
	     cols="80"
	     rows="30"
             name="hjcl_links_to_inject"><?php
$hjcl_links=explode("\n",get_option('hjcl_links_to_inject'));
if(count($hjcl_links) < 3){
 echo "http://dfhu.org/
http://dfhu.org/blog/\n";
}
foreach($hjcl_links as $hjcl){  
  echo trim($hjcl) . "\n";
 
}
          ?></textarea>
	</td>
      </tr>
 
    </table>
 
    <p class="submit">
      <input type="submit" 
	     class="button-primary" 
	     value="<?php _e('Save Changes') ?>" />
    </p>
 
  </form>
</div>
 
<?php
}
 
 
function hijack_comment_links($comment){
  /* 
     Searches through the comment text for and replaces them with one
     of your preset http(s) urls
  */
 
  // really small comments dont, have links, so ...
  if(strlen($comment) < 15){
    // we can just return the comment as is
    return $comment;
  }
 
 
  // We want to seed the random generator. We use the sum of the
  // Ordinal of the 2nd 12th,14th and 10th letters of the comment, why
  // those numbers?
 
  // we want to store the old seed, so that we can re-randomize the
  // PRG again.
  $o_seed=rand();
  srand(
   array_sum(
    array_map(
     'ord',
     array($comment[1],
	   $comment[11],
	   $comment[13],
	   $comment[10]))));
 
  // The links are stored, one per line, so we explode over the
  // newline char.
  $links=explode("\n",get_option('hjcl_links_to_inject'));
 
  // Lets make sure we have links, but if we dont ...
  if(!is_array($links)){
    // ... then just return the comment as is.
    return $comment;
  }
 
  // hey! we have links, long comments, lets get a random url
  $link=$links[array_rand($links)];
 
  // hey random number generator, here is your seed back.
  srand($o_seed); 
 
 
  // We can check to see if we are going get all crazy and remove no
  // follows, so our link juice sales.
  if(get_option('hjcl_remove_nofollow') == 1){
    // link juice be sailing, so nofollow is a no gone.
    $comment=preg_replace("/nofollow/","",$comment);
  }
 
  // the most flawless regex ever created by mankind to catch a
  // http(s) url, /sarcasm. If you don't like this regex you can
  // spruce it up to your likings. Or maybe you could do something
  // with var_filter(), or whatever makes you happy, for me, here is
  // the the regex.
  $r=';https?://[^" ]+;';
 
  // Now just replace all the urls in the comment with our url of
  // random choice.
  return preg_replace($r,$link.'"',$comment);
}
 
 
// Before the links get iserted we hijack the links if any. We call as
// the very last step, before the comment gets packed into the db.
add_filter('preprocess_comment', 'hijack_comment_links', 100);
 
 
// If you want the injection to work retroactively on old posts, This
// will results in links being randomized everytime you edit your
// injected link list, its not recommened.
 
/**/
if(get_option('hjcl_retro') == 1){
  add_filter('comment_text', 'hijack_comment_links', 100);
  add_filter('comment_text_rss', 'hijack_comment_links', 100);
  add_filter('comment_excerpt', 'hijack_comment_links', 100);
}
/**/
 
?>

So that about does it for today, I don’t wan to make sure that i mix it up between coding, marketing and lifestyle posts.

I am still working on the title tweaker posts and am asking for feedback over at the SQUIRT forum.

Also i will be getting on with redirects seeing as people told me that it was useful. Sometimes the most basic stuff is the most powerful.

You can try and catch me on Skype using the username thrilling_victory if you have questions or comments or just want to network.

Share
This entry was posted in beginner-programming. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

If you are going to post code please use:
<pre lang="php" escaped="true"> YOUR_CODE_HERE </pre>

Change the lang to mysql, python, lisp, whatever. This will escape your code.