I see that many people's blogs have this "magical" feature. As a website that has been filed, I think it is indeed necessary. After all, we need to be careful. But if the deployment is in that place and there is no that thing, I personally feel that whether it exists or not is not that important. What does it have to do with me where it redirects to? But since others have it and I don't, do I feel like something is missing (this mentality is really not good, but it gives me a reason to tinker again).
As a senior novice, it is impossible for me to finish writing the code by myself. But now we have ChatGpt, this thing has helped me countless times. Here, I would like to express my sincere thanks to OpenAI. You guys are awesome! Okay, let's get down to business without much ado. To make all links (articles, comments) redirect to an intermediate page (here using go.php), it is not difficult. Add the corresponding code in the functions.php file of the theme:
/* Link redirection */
function redirect_external_links($content) {
$home_url = home_url(); // Get the main URL of the WordPress site
// Use regular expressions to match all links
return preg_replace_callback('#<a\s+[^>]*href=["\'](.*?)["\'][^>]*>#is', function($matches) use ($home_url) {
$url = $matches[1]; // Extract the URL
// Check if it is an external link
if (strpos($url, $home_url) === false && filter_var($url, FILTER_VALIDATE_URL)) {
// Modify the URL to redirect through go.php
$new_url = '/go.php?url=' . urlencode($url);
return str_replace($matches[1], $new_url, $matches[0]);
} else {
// Do not modify internal links
return $matches[0];
}
}, $content);
}
// Add a filter to process links in the content of articles
add_filter('the_content', 'redirect_external_links');
// Process links in comments
add_filter('comment_text', 'redirect_external_links');
// Process links in comment author
function filter_comment_author_link($author_link) {
$home_url = get_option('home');
return redirect_external_links($author_link);
}
add_filter('get_comment_author_link', 'filter_comment_author_link');
Then create a go.php file in the root directory of WordPress, the specific reference code is as follows:
<?php
// Load the WordPress environment and template
require_once('wp-load.php');
// Security check to ensure that the passed URL is valid and prevent redirect attacks
$raw_url = isset($_GET['url']) ? $_GET['url'] : 'http://057000.xyz';
$url = filter_var($raw_url, FILTER_VALIDATE_URL) ? $raw_url : 'http://057000.xyz';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Page Redirect--Feng's Blog</title>
<style>
body {
padding: 0;
margin: 0;
font-family: Arial, sans-serif;
background: #f1f1f1;
text-align: left;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
background: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.url, .countdown {
margin: 10px 0;
}
a.button {
background: #3498db;
color: white;
padding: 10px 20px;
text-decoration: none;
border-radius: 5px;
transition: background-color 0.3s;
}
a.button:hover {
background: #2980b9;
}
</style>
<script>
var countdown = 10; // Countdown in seconds
var url = '';
function redirectToUrl() {
window.location.href = url;
}
function updateCountdown() {
var countdownElement = document.getElementById('countdown');
countdownElement.innerHTML = `Will automatically open in ${countdown} seconds...`;
if(countdown === 0) {
redirectToUrl();
} else {
countdown--;
setTimeout(updateCountdown, 2000);
}
}
window.onload = function() {
const params = new URLSearchParams(window.location.search);
url = params.get('url') || 'http://057000.xyz'; // Default URL if none provided
var urlElement = document.getElementById('url');
urlElement.innerHTML = `<strong>Will visit:</strong> ${url}`;
setTimeout(updateCountdown, 2000); // Start countdown
};
</script>
</head>
<body>
<div class="container">
<h1>Leaving Feng's Blog...</h1>
<p class="url" id="url"></p>
<p>You are about to be redirected, please pay attention to the security of your account and property.</p>
<p class="countdown" id="countdown"></p>
<a href="#" onclick="redirectToUrl();" class="button">Continue to visit</a>
</div>
</body>
</html>
Remember to change 057000.xyz in the above code to your own blog address, and the approximate effect is as follows:
Okay, we have our own redirect page now, haha.