Hướng dẫn thêm rel="nofollow" và target="_blank" cho wordpress không dùng plugin

Admin

AdminAdmin is verified member.

Well-Known Member
Staff member
Administrator
Demo ảnh
07qq.png

Đăng nhập host, mở tập tin functions.php trong theme của bạn, dán code này vào
PHP:
add_filter( 'the_content', 'cn_nf_url_parse');

function cn_nf_url_parse( $content ) {

    $regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>";
    if(preg_match_all("/$regexp/siU", $content, $matches, PREG_SET_ORDER)) {
        if( !empty($matches) ) {

            $srcUrl = get_option('siteurl');
            for ($i=0; $i < count($matches); $i++)
            {

                $tag = $matches[$i][0];
                $tag2 = $matches[$i][0];
                $url = $matches[$i][0];

                $noFollow = '';

                $pattern = '/target\s*=\s*"\s*_blank\s*"/';
                preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
                if( count($match) < 1 )
                    $noFollow .= ' target="_blank" class="ext"';

                $pattern = '/rel\s*=\s*"\s*[n|d]ofollow\s*"/';
                preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
                if( count($match) < 1 )
                    $noFollow .= ' rel="nofollow" ';

                $pos = strpos($url,$srcUrl);
                if ($pos === false) {
                    $tag = rtrim ($tag,'>');
                    $tag .= $noFollow.'>';
                    $content = str_replace($tag2,$tag,$content);
                }
            }
        }
    }

    $content = str_replace(']]>', ']]&gt;', $content);
    return $content;

}
Lưu lại là xong
Chúc các bạn thành công!
 
Cài kiểu này link ngoài site mình là nofolow còn link site mình là dofolow à admin
 

Facebook Comments

Similar threads

T
Replies
1
Views
3K
AdminAdmin is verified member.
Admin
Back
Top