How to add Nofollow attribute to WordPress affiliate link?Blog Friendship Chain Set Nofollow Tag

WordPress backendThe default link option for , usually used as an affiliate link.

Among them, "Link Relationship (XFN)" provides webmasters with many link relationship options, such as: workplace relationship, geographic relationship, family relationship, emotional relationship...but these are not very useful to us.

WordPress Blog Friends Chain Set Nofollow Tag

When adding affiliate links and external links to a WordPress blog, we may need to add the "nofollow" attribute to other external links.

This possible link relationship is not available by default, so we need to add the nofollow tag ourselves.

For the "nofollow" attribute and the function and effect of rel="noopener", if you don't understand, you can click the link below to view the relevant instructions ▼

How to Add Nofollow Attribute to WordPress Affiliate Links?

Add the following code directly to the WordPress theme you are usingfunctions.phpthe last of the file ?> and save ▼

/**
* WordPress友情链接添加nofollow属性标签
* https://www.chenweiliang.com/cwl-28448.html
* 添加两个钩子是为了确保代码只在links页面显示
* 如果你想了解更多load-$page action的信息,访问http://codex.wordpress.org/Adding_Administration_Menus#Page_Hook_Suffix
**/
add_action('load-link.php', 'sola_blogroll_nofollow');
add_action('load-link-add.php', 'sola_blogroll_nofollow');

function sola_blogroll_nofollow() {
//通过action add_meta_boxes创建我们需要的Meta Box
add_action('add_meta_boxes', 'sola_blogroll_add_meta_box', 1, 1);
//通过filter pre_link_rel将数据保存
add_filter('pre_link_rel', 'sola_blogroll_save_meta_box', 10, 1);
}

//创建Nofollow Meta Box
function sola_blogroll_add_meta_box() {
//翻译成中文就是,创建一个名叫Blogroll Nofollow的Meta Box,放在link页面的右侧边栏,Meta Box的结构
//由函数sola_blogroll_inner_meta_box产生
add_meta_box('sola_blogroll_nofollow_div', __('Nofollow标签'), 'sola_blogroll_inner_meta_box', 'link', 'side');
}

//输出Meta Box的HTML结构
function sola_blogroll_inner_meta_box($post) {
$bookmark = get_bookmark($post->ID, 'ARRAY_A');
if (strpos($bookmark['link_rel'], 'nofollow') !== FALSE)
$checked = ' checked="checked"';
else
$checked = '';
?>
<label for="sola_blogroll_nofollow_checkbox"><?php echo __('是否添加Nofollow标签?'); ?></label>
<input value="1" id="sola_blogroll_nofollow_checkbox" name="sola_blogroll_nofollow_checkbox"<?php echo $disabled; ?> type="checkbox"<?php echo $checked; ?> /> <?php echo $message; ?>
<?php
}

//保存用户的选择
function sola_blogroll_save_meta_box($link_rel) {
$rel = trim(str_replace('nofollow', '', $link_rel));
if ($_POST['sola_blogroll_nofollow_checkbox'])
$rel .= ' nofollow';
return trim($rel);
}

Then, on the link editing page, you can see whether to add the setting option of the Nofollow attribute label ▼

How to add Nofollow attribute to WordPress affiliate link?Blog Friendship Chain Set Nofollow Tag

All of the above methods can be used for actual testing.

Hope Chen Weiliang Blog ( https://www.chenweiliang.com/ ) shared "How to Add Nofollow Attribute to WordPress Affiliate Links?Blog Friends Chain Set Nofollow Tag", it will help you.

Welcome to share the link of this article:https://www.chenweiliang.com/cwl-28448.html

Welcome to the Telegram channel of Chen Weiliang's blog to get the latest updates!

🔔 Be the first to get the valuable "ChatGPT Content Marketing AI Tool Usage Guide" in the channel top directory! 🌟
📚 This guide contains huge value, 🌟This is a rare opportunity, don’t miss it! ⏰⌛💨
Share and like if you like!
Your sharing and likes are our continuous motivation!

 

Comment

Your email address will not be published. Required fields * Callout

scroll to top