Custom parser for WP-NoExternalLinks

Warning! I no longer support this plugin after version 3.5.9.9! Please ask questions on wordpress support page.

Recently I added a new feature to this plugin. Now you can extend it yourself without danger of plugin update which could remove all your changes in plugin code.

In this sample, we will overwrite function check_follow, which checks if link is posted by admin and has rel=”follow” attribute. Let’s imagine we want all our authors to have the same options. How can we accomplish it? Easily! Just create file custom-parser.php in directory wp-content/uploads and replace

user_can($author,'manage_options' );
from original function with
user_can($author,'publish_posts' );
- voila!

Here is the full code of our new modified class which will be loaded instead of basic one:

wp-content/uploads/custom-parser.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
if(!defined('DB_NAME'))
die('Error: Plugin "wp-noexternallinks" does not support standalone calls, damned hacker.');

#include base parser
include_once(ABSPATH . 'wp-content/plugins/wp-noexternallinks/wp-noexternallinks-parser.php');

class custom_parser extends wp_noexternallinks_parser
{
function check_follow($matches)
{
#support of "meta=follow" option for admins. disabled by default to minify processing.
if(!$this->options['dont_mask_admin_follow'])
return false;
$id=array(get_comment_ID(),get_the_ID());//it is either page or post
if($id[0])
$this->debug_info('It is a comment. id '.$id[0]);
elseif($id[1])
$this->debug_info('It is a page. id '.$id[1]);
$author=false;
if($id[0])
$author=get_comment_author($id[0]);
else if($id[1])
$author=get_the_author_meta('ID');
if(!$author)
$this->debug_info('it is neither post or page, applying usual rules');
elseif(user_can($author,'publish_posts' )&&(stripos($matches[0],'rel="follow"')!==FALSE || stripos($matches[0],"rel='follow'")!==FALSE))
{
$this->debug_info('This link has a follow atribute and is posted by author, not masking it.');
#wordpress adds rel="nofollow" by itself when posting new link in comments. get rid of it! Also, remove our follow attibute - it is unneccesary.
return str_ireplace(array('rel="follow"',"rel='follow'",'rel="nofollow"'),'',$matches[0]);
}
else
$this->debug_info('it does not have rel follow or is not posted by author, masking it');
return false;
}
}
?>

One more sample is encrypting links.