homepage Welcome to WebmasterWorld Guest from 54.227.41.242
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Help in adding if else conditions
sledge81




msg:4291477
 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');

 

coopster




msg:4305470
 4:24 pm on Apr 28, 2011 (gmt 0)

Are you getting any error messages? What seems to be the issue?

sledge81




msg:4305491
 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!

coopster




msg:4305575
 7:36 pm on Apr 28, 2011 (gmt 0)

What was the solution?

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved