Welcome to WebmasterWorld Guest from 54.147.217.76

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)



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)

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



Are you getting any error messages? What seems to be the issue?
4:46 pm on Apr 28, 2011 (gmt 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)

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



What was the solution?