wordpress后台链接添加nofollow选项

Author: 陌小雨Date: 2020-09-03View: 92

wordpress网址导航主题wpsite用到了这个功能,有兴趣的可以前往了解

Wpsite-P

add_action('load-link.php', 'wpsite_blogroll_nofollow');
add_action('load-link-add.php', 'wpsite_blogroll_nofollow');
function wpsite_blogroll_nofollow() {
    //通过action add_meta_boxes创建我们需要的Meta Box
    add_action('add_meta_boxes', 'wpsite_blogroll_add_meta_box', 1, 1);
    //通过filter pre_link_rel将数据保存
    add_filter('pre_link_rel', 'wpsite_blogroll_save_meta_box', 10, 1);
}
//创建Nofollow Meta Box
function wpsite_blogroll_add_meta_box() {
    //翻译成中文就是,创建一个名叫Blogroll Nofollow的Meta Box,放在link页面的右侧边栏,Meta Box的结构
    //由函数sola_blogroll_inner_meta_box产生
    add_meta_box('wpsite_blogroll_nofollow_div', __('Nofollow标签'), 'wpsite_blogroll_inner_meta_box', 'link', 'side');
}
//输出Meta Box的HTML结构
function wpsite_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="wpsite_blogroll_nofollow_checkbox"><?php echo __('是否添加Nofollow标签?'); ?></label>
    <input value="1" id="wpsite_blogroll_nofollow_checkbox" name="wpsite_blogroll_nofollow_checkbox"<?php echo $disabled; ?> type="checkbox"<?php echo $checked; ?> /> <?php echo $message; ?>
    <?php
}
//保存用户的选择
function wpsite_blogroll_save_meta_box($link_rel) {
    $rel = trim(str_replace('nofollow', '', $link_rel));
    if ($_POST['wpsite_blogroll_nofollow_checkbox'])
        $rel .= ' nofollow';
    return trim($rel);
}

之前小雨也分享过实现这个功能的插件

WordPress插件推荐:Add-nofollow-to-XFN