How To Add Nofollow to PHP Links ?

Discussion in 'PHP' started by crystak, Jan 6, 2008.

  1. #1
    I want to add the nofollow tag to the comments links on my website: http://www.hannahmontanazone.com both for individual posts and for the comment links in the footer.

    I think it would be good because I have way too many links on one page. So anyway, the following bit of code determines these contents:

    <?php if (function_exists('mdv_recent_comments')) { ?>
    <div class="Cols" style="margin:0px 30px">
    <h3>Recent Comments</h3>
     <ul>
      <?php mdv_recent_comments('10'); ?>
     </ul>
    </div>
    <?php } ?>
    PHP:
    How can I add nofollow to the links produced by this code ?
     
    crystak, Jan 6, 2008 IP
  2. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #2
    just on the comment page where these links are appearing in <head> section of page use Meta tag for nofollow.

    Regards

    Alex
     
    kmap, Jan 7, 2008 IP
  3. KatieK

    KatieK Active Member

    Messages:
    116
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    58
    #3
    If you want only certain links on the page to have the nofollow attribute, you'll need to edit the mdv_recent_comments function. Can you show us the contents of that function?
     
    KatieK, Jan 7, 2008 IP
  4. crystak

    crystak Well-Known Member

    Messages:
    1,121
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    115
    #4
    Yes, I only want these comments to have nofollow links so I can at least minimize the outgoing links on my page. Here is that function I think:

    /*
    Plugin Name: Recent Comments
    Plugin URI: http://mtdewvirus.com/code/wordpress-plugins/
    */
    if (function_exists('mdv_recent_comments')) { 
    }else{
    		
    	function mdv_recent_comments($no_comments = 10, $comment_lenth = 5, $before = '<li>', $after = '</li>', $show_pass_post = false, $comment_style = 0) {
    	    global $wpdb;
    	    $request = "SELECT ID, comment_ID, comment_content, comment_author, comment_author_url, post_title FROM $wpdb->comments LEFT JOIN $wpdb->posts ON $wpdb->posts.ID=$wpdb->comments.comment_post_ID WHERE post_status IN ('publish','static') ";
    		if(!$show_pass_post) $request .= "AND post_password ='' ";
    		$request .= "AND comment_approved = '1' ORDER BY comment_ID DESC LIMIT $no_comments";
    		$comments = $wpdb->get_results($request);
    	    $output = '';
    		if ($comments) {
    			foreach ($comments as $comment) {
    				$comment_author = stripslashes($comment->comment_author);
    				if ($comment_author == "")
    					$comment_author = "anonymous"; 
    				$comment_content = strip_tags($comment->comment_content);
    				$comment_content = stripslashes($comment_content);
    				$words=split(" ",$comment_content); 
    				$comment_excerpt = join(" ",array_slice($words,0,$comment_lenth));
    				$permalink = get_permalink($comment->ID)."#comment-".$comment->comment_ID;
    	
    				if ($comment_style == 1) {
    					$post_title = stripslashes($comment->post_title);
    					
    					$url = $comment->comment_author_url;
    	
    					if (empty($url))
    						$output .= $before . $comment_author . ' on ' . $post_title . '.' . $after;
    					else
    						$output .= $before . "<a href='$url' rel='external'>$comment_author</a>" . ' on ' . $post_title . '.' . $after;
    				}
    				else {
    					$output .= $before . '' . $comment_author . ':  <a href="' . $permalink;
    					$output .= '" title="View the entire comment by ' . $comment_author.'">' . $comment_excerpt.'</a>' . $after;
    				}
    			}
    			$output = convert_smilies($output);
    		} else {
    			$output .= $before . "None found" . $after;
    		}
    	    echo $output;
    	}
    }
    
    PHP:
    Is this the right one ? This is for wordpress by the way so I took it from the theme file.
     
    crystak, Jan 7, 2008 IP
  5. wdstuff54

    wdstuff54 Well-Known Member

    Messages:
    639
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    110
    #5
    Where it says
    change to:
    Hope that helps.
     
    wdstuff54, Jan 7, 2008 IP
  6. wdstuff54

    wdstuff54 Well-Known Member

    Messages:
    639
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    110
    #6
    Come to think of it you might need it on the
    also. But I'm not sure you can have two "rel" attributes in one link.
     
    wdstuff54, Jan 7, 2008 IP
  7. KatieK

    KatieK Active Member

    Messages:
    116
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    58
    #7
    This is what I would do:

    Insert this copy of the existing mdv_recent_comments function, just below where the original mdv_recent_comments function is defined.

    
    /*
    Plugin Name: Recent Comments
    Plugin URI: http://mtdewvirus.com/code/wordpress-plugins/
    Edited to add attribute nofollow
    */
    if (function_exists('mdv_recent_comments_nofollow')) {
    }else{
           
        function mdv_recent_comments($no_comments = 10, $comment_lenth = 5, $before = '<li>', $after = '</li>', $show_pass_post = false, $comment_style = 0) {
            global $wpdb;
            $request = "SELECT ID, comment_ID, comment_content, comment_author, comment_author_url, post_title FROM $wpdb->comments LEFT JOIN $wpdb->posts ON $wpdb->posts.ID=$wpdb->comments.comment_post_ID WHERE post_status IN ('publish','static') ";
            if(!$show_pass_post) $request .= "AND post_password ='' ";
            $request .= "AND comment_approved = '1' ORDER BY comment_ID DESC LIMIT $no_comments";
            $comments = $wpdb->get_results($request);
            $output = '';
            if ($comments) {
                foreach ($comments as $comment) {
                    $comment_author = stripslashes($comment->comment_author);
                    if ($comment_author == "")
                        $comment_author = "anonymous";
                    $comment_content = strip_tags($comment->comment_content);
                    $comment_content = stripslashes($comment_content);
                    $words=split(" ",$comment_content);
                    $comment_excerpt = join(" ",array_slice($words,0,$comment_lenth));
                    $permalink = get_permalink($comment->ID)."#comment-".$comment->comment_ID;
       
                    if ($comment_style == 1) {
                        $post_title = stripslashes($comment->post_title);
                       
                        $url = $comment->comment_author_url;
       
                        if (empty($url))
                            $output .= $before . $comment_author . ' on ' . $post_title . '.' . $after;
                        else
                            $output .= $before . "<a href='$url' rel='external nofollow'>$comment_author</a>" . ' on ' . $post_title . '.' . $after;
                    }
                    else {
                        $output .= $before . '' . $comment_author . ':  <a href="' . $permalink;
                        $output .= '" title="View the entire comment by ' . $comment_author.'" rel="nofollow">' . $comment_excerpt.'</a>' . $after;
                    }
                }
                $output = convert_smilies($output);
            } else {
                $output .= $before . "None found" . $after;
            }
            echo $output;
        }
    }
    
    Code (markup):
    I've edited the mdv_recent_comments function by adding nofollow to the code on lines 35 and 39.

    Then, every time you want to use the new function, use this block of code in place of what you posted originally:

    
    <?php if (function_exists('mdv_recent_comments_nofollow')) { ?>
    <div class="Cols" style="margin:0px 30px">
    <h3>Recent Comments</h3>
     <ul>
      <?mdv_recent_comments_nofollow('10'); ?>
     </ul>
    </div>
    <?php } ?>
    
    Code (markup):
    Using a copy of the function should prevent many adverse side effects, but test it first, of course. :)

    Note that having multiple rel= attributes on an element would be invalid, but it is valid to have both external and nofollow inside a single attribute.
     
    KatieK, Jan 8, 2008 IP
    crystak likes this.