/home/tu-dong-them-thuoc-tinh-nofollow-wordpress

Tự động thêm thuộc tính "Nofollow" cho external link

Published on | Updated

Mặc định, Wordpress không tự thêm thuộc tính rel="nofollow" vào external link trong post content. Nếu bạn muốn làm điều này, đây là một thủ thuật đơn giản bắt Wordpress thêm thuộc tính rel="nofollow" vào mọi external link.

Dán đoạn mã dưới đây vào tập tin functions.php. Sau khi lưu, tất cả external link trong post content của bạn sẽ được thay đổi thành nofollow.


add_filter('the_content', 'auto_nofollow');<br></br>
<br></br>
function auto_nofollow($content) {<br></br>
    //return stripslashes(wp_rel_nofollow($content));<br></br>
<br></br>
    return preg_replace_callback('/<a>]+/', 'auto_nofollow_callback', $content);<br></br>
}<br></br>
<br></br>
function auto_nofollow_callback($matches) {<br></br>
    $link = $matches[0];<br></br>
    $site_link = get_bloginfo('url');<br></br>
<br></br>
    if (strpos($link, 'rel') === false) {<br></br>
        $link = preg_replace("%(href=S(?!$site_link))%i", 'rel="nofollow" $1', $link);<br></br>
    } elseif (preg_match("%href=S(?!$site_link)%i", $link)) {<br></br>
        $link = preg_replace('/rel=S(?!nofollow)S*/i', 'rel="nofollow"', $link);<br></br>
    }<br></br>
    return $link;<br></br>
}<br></br>

Chú thích: external link là liên kết không thuộc tên miền của bạn. Ví dụ blog của bạn có tên miền là "abc.com" thì mọi liên kết khác abc.com gọi là external link