在WordPress網(wǎng)站中,當(dāng)用戶點(diǎn)擊圖片鏈接跳轉(zhuǎn)到外部網(wǎng)站時(shí),默認(rèn)會(huì)傳遞refer信息(即來源頁面信息)。有時(shí)出于隱私保護(hù)或其他原因,站長(zhǎng)可能希望移除這些refer信息。下面介紹幾種實(shí)現(xiàn)方法。
方法一:使用rel=“noreferrer”屬性
最簡(jiǎn)單的方法是為圖片鏈接添加rel="noreferrer"
屬性:
- 在WordPress編輯器中,切換到HTML/文本模式
- 找到圖片鏈接代碼,添加
rel="noreferrer"
屬性 - 示例代碼:
<a href="https://example.com" rel="noreferrer">
<img src="image.jpg" alt="示例圖片">
</a>
方法二:使用插件實(shí)現(xiàn)
對(duì)于不熟悉代碼的用戶,可以使用插件批量處理:
- 安裝”External Links”或”Nofollow for External Link”等插件
- 在插件設(shè)置中啟用”noreferrer”選項(xiàng)
- 插件會(huì)自動(dòng)為所有外部鏈接添加noreferrer屬性
方法三:通過functions.php添加代碼
對(duì)于技術(shù)用戶,可以在主題的functions.php文件中添加以下代碼:
function add_noreferrer_to_external_links($content) {
$content = preg_replace_callback('/<a[^>]+/', function($matches) {
$link = $matches[0];
if(strpos($link, 'rel=') === false) {
$link = preg_replace("/<a\s+/", '<a rel="noreferrer" ', $link);
} elseif(preg_match('/rel=["\']([^"\']*)["\']/', $link, $rel_matches)) {
$rels = array_map('trim', explode(' ', $rel_matches[1]));
if(!in_array('noreferrer', $rels)) {
$rels[] = 'noreferrer';
$new_rel = 'rel="'.implode(' ', $rels).'"';
$link = str_replace($rel_matches[0], $new_rel, $link);
}
}
return $link;
}, $content);
return $content;
}
add_filter('the_content', 'add_noreferrer_to_external_links');
注意事項(xiàng)
- 移除refer信息可能會(huì)影響部分網(wǎng)站的統(tǒng)計(jì)功能
- 某些網(wǎng)站可能會(huì)拒絕沒有refer信息的訪問
- 建議僅對(duì)確實(shí)需要保護(hù)用戶隱私的鏈接使用此功能
通過以上方法,您可以有效控制WordPress網(wǎng)站中圖片鏈接的refer信息傳遞,平衡用戶體驗(yàn)與隱私保護(hù)的需求。