Guest User

Nofollow for external link / modified to remove 'dofollow'

a guest
Feb 6th, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.57 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Nofollow for external link / modified to remove 'dofollow'
  4. Plugin URI: http://www.cybernetikz.com
  5. Description: Just simple, if you use this plugins, `rel=&quot;nofollow&quot;` and `target=&quot;_blank&quot;` will be added automatically, for all the external links of your website posts or pages.
  6. Version: 1.0.1
  7. Author: CyberNetikz MOD
  8. Author URI: http://www.cybernetikz.com
  9. License: GPL2
  10. */
  11.  
  12. add_filter( 'the_content', 'cn_nf_url_parse');
  13.  
  14. function cn_nf_url_parse( $content ) {
  15.  
  16.     $regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>";
  17.     if(preg_match_all("/$regexp/siU", $content, $matches, PREG_SET_ORDER)) {
  18.         if( !empty($matches) ) {
  19.            
  20.             $srcUrl = get_option('siteurl');
  21.             for ($i=0; $i < count($matches); $i++)
  22.             {
  23.            
  24.                 $tag = $matches[$i][0];
  25.                 $tag2 = $matches[$i][0];
  26.                 $url = $matches[$i][0];
  27.                
  28.                 $noFollow = '';
  29.  
  30.                 $pattern = '/target\s*=\s*"\s*_blank\s*"/';
  31.                 preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
  32.                 if( count($match) < 1 )
  33.                     $noFollow .= ' target="_blank" ';
  34.                    
  35.                 $pattern = '/rel\s*=\s*"\s*[n|d]ofollow\s*"/';
  36.                 preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
  37.                 if( count($match) < 1 )
  38.                     $noFollow .= ' rel="nofollow" ';
  39.            
  40.                 $pos = strpos($url,$srcUrl);
  41.                 if ($pos === false) {
  42.                     $tag = rtrim ($tag,'>');
  43.                     $tag .= $noFollow.'>';
  44.                     $content = str_replace($tag2,$tag,$content);
  45.                 }
  46.             }
  47.         }
  48.     }
  49.    
  50.     $content = str_replace(']]>', ']]&gt;', $content);
  51.  
  52.         $content = str_replace("rel=\"dofollow\"", "", $content);
  53.  
  54.     return $content;
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment