Page is a not externally linkable
sledge81 - 3:10 pm on Apr 3, 2011 (gmt 0)
I'm not that well versed in PHP, hoping someone could give me the fix.
I have two plugins. One adds nofollow tag to all external links, the other plugin adds a dofollow when a post has a custom field called dofollow with value =1.
I'm trying to mix these two together so that if there is no dofollow field, then by default the nofollow comes to play and vice versa.
Hoping some PHP Guru will help me with this.
No Follow Code: <?php
if (!class_exists('ExternalNofollow')) {
class ExternalNofollow
{
static function init()
{
add_filter('the_content', array(__CLASS__, 'nofollow'));
}
function nofollow($content)
{
//return stripslashes(wp_rel_nofollow($content));
return preg_replace_callback('/<a[^>]+/', array(__CLASS__, 'callback'), $content);
}
function singlePostDoFollow($content){
global $post;
if(get_post_meta($post->ID,"dofollow",true) === 1){
//return str_replace('rel="nofollow"','',$content);
return preg_replace('~<a ([^>]*)\s*(["|\']{1}\w*)\s*nofollow([^>]*)>~U','<a ${1}${2}${3}>', $content);
}else{
return $content;
}
}
function callback($matches)
{
$link = $matches[0];
$site_link = get_bloginfo('url');
if (strpos($link, 'rel') === false) {
$link = preg_replace("%(href=\S(?!$site_link))%i", 'rel="nofollow" $1', $link);
} elseif (preg_match("%href=\S(?!$site_link)%i", $link)) {
$link = preg_replace('/rel=\S(?!nofollow)\S*/i', 'rel="nofollow"', $link);
}
return $link;
}
}
ExternalNofollow::init();
}
?>
DoFollow Code
function singlePostDoFollow($content){
global $post;
if(get_post_meta($post->ID,"dofollow",true) == 1){
//return str_replace('rel="nofollow"','',$content);
return preg_replace('~<a ([^>]*)\s*(["|\']{1}\w*)\s*nofollow([^>]*)>~U','<a ${1}${2}${3}>', $content);
}else{
return $content;
}
}
add_filter('the_content', 'singlePostDoFollow');