Welcome to WebmasterWorld Guest from 54.196.244.186

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Help in adding if else conditions

     
3:10 pm on Apr 3, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Oct 6, 2010
posts: 53
votes: 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');
4:24 pm on Apr 28, 2011 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12533
votes: 0


Are you getting any error messages? What seems to be the issue?
4:46 pm on Apr 28, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Oct 6, 2010
posts: 53
votes: 0


Hey Coopster.. Thanks, but I managed to figure this out. If there's an option to close this thread or mark as resolved.

Cheers!
7:36 pm on Apr 28, 2011 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12533
votes: 0


What was the solution?