纯代码实现关于WordPress外链转内链,添加跳转提醒,今天介绍两种方法实现自动,手动,自定义转换。

要建立高质量的外链,需要选择具有较高权威度和知名度的网站进行链接。同时,外链的数量也不应过多,过多的外链可能会降低网站排名。为了获得更好的外链效果,可以通过发布高质量的内容来吸引其他网站链接到你的网站。

第一种是在WordPress下载插件类似的插件非常多这里就推荐一个

名称:Smart SEO Tool-WordPress SEO优化插件

安装启动进行一些简单的配置就可以完成就可以自动实现,

2024112613372621

另一个插件是不过需要手动配置添加页面也是个不错的选择

作者:Inisev重定向名称:Redirection

类似的插件有很多这里就不一一列举了,

最后就是通过代码实现自动转换无需安装插件即可完成

在你当前使用主题的 functions.php 中加入以下代码:

add_filter('the_content','baezone_the_go_url',999);
function baezone_the_go_url($content){
preg_match_all('/href="https://www.wuchens.com"/',$content,$matches);
if($matches){
foreach($matches[1] as $val){
if( strpos($val,home_url())===false ) $content=str_replace("href=\"$val\"", "href=\"" . get_bloginfo('wpurl'). "/go?url=" .base64_encode($val). "\"",$content);
}
}
return $content;
}

把上面内容

https://www.wuchens.com

替换为

(.*?)
网站根目录下新建个名为 go 的文件夹(WordPress根目录),在其中添加文件名称index.php ,内容如下:

<?php
$url = $_GET['url'];
$url = base64_decode($url);
?>
<html>
<head>
<meta charset=utf-8 />
<meta name="robots" content="nofollow">
<meta http-equiv="refresh" content="0.1;url=<?php echo $url; ?>">
<title>正在为您跳转……</title>
<style>
body{background:#000}.loading{-webkit-animation:fadein 2s;-moz-animation:fadein 2s;-o-animation:fadein 2s;animation:fadein 2s}@-moz-keyframes fadein{from{opacity:0}to{opacity:1}}@-webkit-keyframes fadein{from{opacity:0}to{opacity:1}}@-o-keyframes fadein{from{opacity:0}to{opacity:1}}@keyframes fadein{from{opacity:0}to{opacity:1}}.spinner-wrapper{position:absolute;top:0;left:0;z-index:300;height:100%;min-width:100%;min-height:100%;background:rgba(255,255,255,0.93)}.spinner-text{position:absolute;top:41.5%;left:47%;margin:16px 0 0 35px;color:#BBB;letter-spacing:1px;font-weight:700;font-size:9px;font-family:Arial}.spinner{position:absolute;top:40%;left:45%;display:block;margin:0;width:1px;height:1px;border:25px solid rgba(100,100,100,0.2);-webkit-border-radius:50px;-moz-border-radius:50px;border-radius:50px;border-left-color:transparent;border-right-color:transparent;-webkit-animation:spin 1.5s infinite;-moz-animation:spin 1.5s infinite;animation:spin 1.5s infinite}@-webkit-keyframes spin{0%,100%{-webkit-transform:rotate(0deg) scale(1)}50%{-webkit-transform:rotate(720deg) scale(0.6)}}@-moz-keyframes spin{0%,100%{-moz-transform:rotate(0deg) scale(1)}50%{-moz-transform:rotate(720deg) scale(0.6)}}@-o-keyframes spin{0%,100%{-o-transform:rotate(0deg) scale(1)}50%{-o-transform:rotate(720deg) scale(0.6)}}@keyframes spin{0%,100%{transform:rotate(0deg) scale(1)}50%{transform:rotate(720deg) scale(0.6)}}
</style>
</head>
<body>
<div class="loading">
<div class="spinner-wrapper">
<span class="spinner-text">加载中...</span>
<span class="spinner"></span>
</div
</div>
</body>
</html>

最后在robots.txt文件中添加代码防止搜索引擎索引

添加下面内容,一般选择第一个即可

Disallow: /go/

或者下面内容

Disallow: /go?url=*

这样在文章内添加的文件就会自动转换为内链防止外链过多

添加外链确认按钮

只需将添加在 go 的文件夹index.php替换下面内容即可

<?php
if (isset($_GET['url'])) {
    $encoded_url = $_GET['url'];  
    $url = base64_decode($encoded_url);  

    if (filter_var($url, FILTER_VALIDATE_URL)) {
        // 截取并格式化链接,显示50个字符
        $short_url = (strlen($url) > 50) ? substr($url, 0, 50) . '...' : $url;

        echo "
        <html>
        <head>
            <meta charset='utf-8' />
            <title>跳转提醒</title>
            <style>
                body {
                    display: flex;
                    justify-content: center;
                    align-items: flex-start;
                    height: 100vh;
                    margin: 0;
                    background-color: #F0F8FF;
                }
                .confirmation-box {
                    background-color: white;
                    color: black;
                    padding: 40px 30px;
                    border-radius: 30px;
                    position: relative;
                    width: 600px;
                    margin-top: 200px; /* 与顶部的距离 */
                    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
                }
                .custom-text {
                    font-size: 18px;
                    font-weight: bold;
                    color: #333;
                    text-align: left; /* 确保文本靠左 */
                    margin: 0; /* 确保无外边距 */
                    padding: 0; /* 确保无内边距 */
                }
                .link-display {
                    text-align: left; /* 链接内容靠左 */
                    margin: 10px 10; /* 添加上下间距 */
                    font-size: 16px;
                    color: black;
                }
                .link-display a {
                    display: inline-block;
                    overflow: hidden;
                    white-space: nowrap;
                    text-overflow: ellipsis;
                }
                button {
                    background-color: #007bff;
                    color: white;
                    border: none;
                    padding: 10px 15px;
                    border-radius: 20px;
                    cursor: pointer;
                    position: absolute;
                    bottom: 10px;
                    right: 10px;
                }
                button:hover {
                    background-color: #0056b3;
                }
            </style>
        </head>
        <body>
            <div class='confirmation-box'>
                <div class='custom-text'>
                    
                    请确认您将跳转到以下链接:
                </div>
                <div class='link-display'>
                    <a href='$url' target='_blank'>$short_url</a>
                </div>
                <button onclick=\"window.location.href='$url';\">确认跳转</button>
            </div>
        </body>
        </html>
        ";
        exit;
    } else {
        echo "<html><head><meta charset='utf-8'><title>无效的链接</title></head><body><h1>无效的链接,请检查后重试。</h1></body></html>";
        exit;
    }
} else {
    echo "<html><head><meta charset='utf-8'><title>错误</title></head><body><h1>未提供跳转链接。</h1></body></html>";
    exit;
}
?>