混淆 Wordpress 中的链接(用于 SEO 目的)

Obfuscate links in Wordpress (for SEO purpose)

Link 混淆是一个越来越常见的话题,目的是通过屏蔽不重要的 link 来为其他人提供更多权重来改进 SEO。

我正在寻找一种在 Wordpress 中混淆 links 的有效方法,显然是直接在源代码中,例如通过向我的 links 添加一个特殊的 class .

它必须将 <a> 元素变成类似 <span> 的其他东西,没有更多可见的 href 属性,也没有任何实际的 URL , 这样机器人就看不到任何 link.

必须在 呈现源代码之前 完成,而不是在 JavaScript 中覆盖。

示例:

<a href="https://example.com">Hello</a>

变成:

<span data-o="CRYPTEDLINK">Hello</span>

然后一些JS允许点击元素重定向到原来的link。

我最终制作了自己的系统,可以让我轻松混淆任何 link。

将以下代码添加到您子主题的 functions.php 文件中,然后只需将 class“混淆”添加到任何元素以混淆其 link 将其替换为不可读的元素 link.

还要确保编辑上面的样式,或者删除它们并在您自己的 CSS 文件中设置“akn-obf-link”class 的样式,使其看起来像 link 给访客。

/*************************************************************************************\
|* Links obuscation - add class "obfuscate" to any <a> element to obfuscate its link *|
\*************************************************************************************/

// Add this code to your child theme's functions.php file, then just add the class "obfuscate" to any <a> element to obfuscate its link by replacing it with a <span> element with no readable link.
// The obfuscated elements inherits the original <a> element's classes, along with a "akn-obf-link" class, so you might need to add CSS to style the "akn-obf-link" class so that it looks like a link to the visitor, maybe at least to add a cursor:pointer.
// On right click, the obfuscated link will be wrapped with a proper <a> element with the "akn-deobf-link" for a brief moment, so that a proper context menu appears, you can remove that behaviour by setting the "deobfucate_on_right_click" option to false in the code bellow.

// Edit 2022-04-05 - modified regex to allow for html elements and new lines into the <a> element, modified callback so the obfuscated element inherits the original link's classes, modified JS to add mousewheel click and right click options.

add_action('wp_loaded', 'buffer_start');
function buffer_start() {
    ob_start('akn_ofbuscate_buffer');
}
add_action('shutdown', 'buffer_end');
function buffer_end() {
    ob_end_flush();
}
function akn_ofbuscate_buffer($buffer) {
    $result = preg_replace_callback('#<a[^>]+(href=(\"|\')([^\"\']*)(\'|\")[^>]+class=(\"|\')[^\'\"]*obfuscate[^\'\"]*(\"|\')|class=(\"|\')[^\'\"]*obfuscate[^\'\"]*(\"|\')[^>]+href=(\"|\')([^\"\']*)(\'|\"))[^>]*>(.+(?!<a))<\/a>#imUs', function($matches) {
        preg_match('#<a[^>]+class=[\"|\\']([^\\'\"]+)[\"|\\']#imUs',$matches[0],$matches_classes);
        $classes = trim(preg_replace('/\s+/',' ',str_replace('obfuscate','',$matches_classes[1])));
        return '<span class="akn-obf-link'.($classes?' '.$classes:'').'" data-o="'.base64_encode($matches[3]?:$matches[10]).'" data-b="'.((strpos(strtolower($matches[0]),'_blank')!==false)?'1':'0').'">'.$matches[12].'</span>';
    }, $buffer);
    return $result;
}
add_action('wp_footer', 'akn_ofbuscate_footer_js');
function akn_ofbuscate_footer_js() {
    ?>
        <script>
            jQuery(document).ready(function($) {
                // options you can change
                var deobfuscate_on_right_click = true;
                // function to open link on click
                function akn_ofbuscate_clicked($el,force_blank) {
                    if (typeof(force_blank)=='undefined')
                        var force_blank = false;
                    var link = atob($el.data('o'));
                    var _blank = $el.data('b');
                    if (_blank || force_blank)
                        window.open(link);
                    else
                        location.href = link;
                }
                // trigger link opening on click
                $(document).on('click','.akn-obf-link',function() {
                    var $el = $(this);
                    if (!$el.closest('.akn-deobf-link').length)
                        akn_ofbuscate_clicked($el);
                });
                // trigger link openin in new tab on mousewheel click
                $(document).on('mousedown','.akn-obf-link',function(e) {
                    if (e.which==2) {
                        var $el = $(this);
                        if (!$el.closest('.akn-deobf-link').length) {
                            akn_ofbuscate_clicked($el,true);
                            return true;
                        }
                    }
                });
                // deobfuscate link on right click so the context menu is a legit menu with link options
                $(document).on('contextmenu','.akn-obf-link',function(e) {
                    if (deobfuscate_on_right_click) {
                        var $el = $(this);
                        if (!$el.closest('.akn-deobf-link').length) {
                            e.stopPropagation();
                            var link = atob($el.data('o'));
                            var _blank = $el.data('b');
                            $el.wrap('<a class="akn-deobf-link" href="'+link+'"'+(_blank?' target="_BLANK"':'')+'></a>').parent().trigger('contextmenu');
                            setTimeout(function() {
                                $el.unwrap();
                            },10);
                        }
                    }
                });
            });
        </script>
    <?php
}

我也在这个 Pastebin 上分享代码:https://pastebin.com/cXEBSVFn

考虑检查 link 以防万一我更新了上面的代码而忘了在这里更新它