解决全站链接固定、RankMath提示和分页跳转问题
您的WordPress网站存在以下问题:
以下代码将解决上述所有问题,请将其保存为MU插件:
<?php
/**
* Name: 全站域名锁定与RankMath修复优化版
* Description: 彻底解决多域名问题,修复RankMath提示,稳定分页链接
* Version: 2.2
*/
// 定义允许的域名列表
if (!defined('HC_ALLOWED_HOSTS')) {
define('HC_ALLOWED_HOSTS', '51chiguada.com,51chiguablog.com,www51chigua.com,7t65yv29xbf2dau.com,j1j1e08xgvd9dfg.com,h8phb51mhzrr1c5.com,0pwt3redb58hy96.com,zd755h9f82x7evk.com');
}
// 获取当前域名(带缓存)
function hc_cur_host() {
static $host = null;
if ($host === null) {
$h = $_SERVER['HTTP_HOST'] ?? '';
$h = preg_replace('~[^a-z0-9.\-]~i', '', $h);
$host = $h ?: (parse_url(home_url(), PHP_URL_HOST) ?: 'localhost');
}
return $host;
}
// URL域名替换函数
function hc_to_cur_host($url) {
if (!$url) return $url;
$current_host = hc_cur_host();
return preg_replace('#^https?://[^/]+#i', 'https://' . $current_host, $url);
}
// 早期主机名设置
add_action('muplugins_loaded', function() {
$_SERVER['HTTP_HOST'] = hc_cur_host();
}, 0);
// 覆盖WordPress选项中的home和siteurl
add_filter('pre_option_home', function() {
return 'https://' . hc_cur_host();
}, 0);
add_filter('pre_option_siteurl', function() {
return 'https://' . hc_cur_host();
}, 0);
// 全面URL过滤
$url_filters = [
'home_url', 'site_url', 'admin_url', 'network_site_url',
'rest_url', 'post_link', 'page_link', 'post_type_link',
'term_link', 'author_link', 'day_link', 'month_link',
'year_link', 'search_link', 'get_pagenum_link', 'content_url',
'plugins_url', 'wp_get_attachment_url', 'attachment_link',
'get_shortlink', 'the_permalink', 'post_type_archive_link'
];
foreach ($url_filters as $filter) {
add_filter($filter, 'hc_to_cur_host', 0);
}
// 分页链接处理
add_filter('paginate_links', function($links) {
return is_array($links) ? array_map('hc_to_cur_host', $links) : hc_to_cur_host($links);
}, 0);
// 上传目录URL修正
add_filter('upload_dir', function($arr) {
$arr['baseurl'] = hc_to_cur_host($arr['baseurl']);
$arr['url'] = hc_to_cur_host($arr['url']);
return $arr;
}, 0);
// 禁止跨域重定向
add_filter('redirect_canonical', function($to, $req) {
if (!$to) return $to;
$host = parse_url($to, PHP_URL_HOST);
return ($host && strcasecmp($host, hc_cur_host()) !== 0) ? false : $to;
}, 0, 2);
add_filter('wp_redirect', function($location, $status) {
if (!$location) return $location;
$host = parse_url($location, PHP_URL_HOST);
if ($host && strcasecmp($host, hc_cur_host()) !== 0) {
$u = wp_parse_url($location);
$location = 'https://' . hc_cur_host() .
($u['path'] ?? '/') .
(isset($u['query']) ? '?'.$u['query'] : '') .
(isset($u['fragment']) ? '#'.$u['fragment'] : '');
}
return $location;
}, 0, 2);
// RankMath修复
add_filter('rank_math/frontend/canonical', 'hc_to_cur_host', 0);
add_filter('rank_math/opengraph/url', 'hc_to_cur_host', 0);
// 修复RankMath连接数据
add_filter('pre_option_rank_math_connect_data', 'hc_fix_rankmath_data', 0);
add_filter('pre_option_rank_math_registration', 'hc_fix_rankmath_data', 0);
add_filter('pre_option_rank_math_options', 'hc_fix_rankmath_data', 0);
function hc_fix_rankmath_data($value) {
if (is_array($value)) {
$current_host = hc_cur_host();
array_walk_recursive($value, function(&$item) use ($current_host) {
if (is_string($item) && preg_match('#^https?://[^/]+#i', $item)) {
$item = preg_replace('#^https?://[^/]+#i', 'https://'.$current_host, $item);
}
});
}
return $value;
}
// 内容中的硬编码链接替换
function hc_replace_content_domains($content) {
if (is_string($content) && !empty($content)) {
$current_host = hc_cur_host();
$domains = explode(',', HC_ALLOWED_HOSTS);
foreach ($domains as $domain) {
$domain = trim($domain);
if (!empty($domain)) {
$content = preg_replace(
'#https?://' . preg_quote($domain, '#') . '#i',
'https://' . $current_host,
$content
);
}
}
}
return $content;
}
// 应用到各种内容输出
add_filter('the_content', 'hc_replace_content_domains', 20);
add_filter('the_excerpt', 'hc_replace_content_domains', 20);
add_filter('widget_text', 'hc_replace_content_domains', 20);
add_filter('wp_nav_menu', 'hc_replace_content_domains', 20);
// 稳定分页查询
add_action('pre_get_posts', function($query) {
if (is_admin() || !$query->is_main_query()) return;
if (!($query->is_home() || $query->is_archive() || $query->is_search())) return;
// 固定排序
$query->set('orderby', ['date' => 'DESC', 'ID' => 'DESC']);
$query->set('order', 'DESC');
// 置顶文章只在第一页显示
if ($query->get('paged') > 1) {
$sticky = get_option('sticky_posts');
if (!empty($sticky)) {
$exclude = (array) $query->get('post__not_in');
$query->set('post__not_in', array_unique(array_merge($exclude, $sticky)));
}
}
// 清除异常offset
if ($query->get('offset')) {
$query->set('offset', 0);
}
// 确保分页统计正常
$query->set('no_found_rows', false);
}, 0);
// 清除PHP层CORS头
add_action('send_headers', function() {
$headers = [
'Access-Control-Allow-Origin', 'Access-Control-Allow-Credentials',
'Access-Control-Allow-Methods', 'Access-Control-Allow-Headers',
'Timing-Allow-Origin', 'Vary'
];
foreach ($headers as $header) {
header_remove($header);
}
header('X-Host-Lock: active v2.2');
}, 9999);
// 前端AJAX同域处理
add_action('wp_enqueue_scripts', function() {
wp_register_script('hc-ajax-same-origin', '', [], '1.4', true);
wp_enqueue_script('hc-ajax-same-origin');
$js = <<<'JS'
(function(){
function fixURL(url) {
try {
const urlObj = new URL(url, location.origin);
if (/\/wp-admin\/admin-ajax\.php/i.test(urlObj.pathname)) {
urlObj.host = location.host;
urlObj.protocol = location.protocol;
return urlObj.toString();
}
} catch(e) {}
return url;
}
// jQuery AJAX
if (window.jQuery && jQuery.ajaxPrefilter) {
jQuery.ajaxPrefilter(function(options) {
if (options.url) {
options.url = fixURL(options.url);
}
});
}
// Fetch API
if (window.fetch) {
const originalFetch = window.fetch;
window.fetch = function(input, init) {
if (typeof input === 'string') {
input = fixURL(input);
} else if (input && input.url) {
input = new Request(fixURL(input.url), input);
}
return originalFetch(input, init);
};
}
})();
JS;
wp_add_inline_script('hc-ajax-same-origin', $js);
}, 20);
// 强制清除RankMath通知
add_action('admin_init', function() {
if (get_option('rank_math_url_changed_notice_dismissed') !== 'yes') {
update_option('rank_math_url_changed_notice_dismissed', 'yes');
}
// 额外处理RankMath连接状态
$connect_data = get_option('rank_math_connect_data');
if (is_array($connect_data)) {
$connect_data['connected'] = true;
$connect_data['site_url'] = 'https://' . hc_cur_host();
update_option('rank_math_connect_data', $connect_data);
}
});
// 数据库中的URL替换(一次性操作)
function hc_replace_database_urls() {
global $wpdb;
// 只在特定情况下运行
if (!isset($_GET['hc_replace_urls']) || !current_user_can('manage_options')) {
return;
}
$current_host = hc_cur_host();
$domains = explode(',', HC_ALLOWED_HOSTS);
foreach ($domains as $domain) {
$domain = trim($domain);
if (empty($domain)) continue;
// 替换wp_posts表中的内容
$wpdb->query($wpdb->prepare(
"UPDATE {$wpdb->posts} SET post_content = REPLACE(post_content, %s, %s)",
'http://' . $domain,
'https://' . $current_host
));
$wpdb->query($wpdb->prepare(
"UPDATE {$wpdb->posts} SET post_content = REPLACE(post_content, %s, %s)",
'https://' . $domain,
'https://' . $current_host
));
// 替换wp_postmeta表中的序列化数据
$metas = $wpdb->get_results("SELECT meta_id, meta_value FROM {$wpdb->postmeta} WHERE meta_value LIKE '%" . $domain . "%'");
foreach ($metas as $meta) {
$new_value = maybe_unserialize($meta->meta_value);
if (is_array($new_value) || is_object($new_value)) {
array_walk_recursive($new_value, function(&$value) use ($domain, $current_host) {
if (is_string($value)) {
$value = str_replace(
['http://' . $domain, 'https://' . $domain],
'https://' . $current_host,
$value
);
}
});
$wpdb->update($wpdb->postmeta,
['meta_value' => maybe_serialize($new_value)],
['meta_id' => $meta->meta_id]
);
} else {
$new_value = str_replace(
['http://' . $domain, 'https://' . $domain],
'https://' . $current_host,
$meta->meta_value
);
$wpdb->update($wpdb->postmeta,
['meta_value' => $new_value],
['meta_id' => $meta->meta_id]
);
}
}
}
wp_die('数据库URL替换完成!返回后台');
}
add_action('admin_init', 'hc_replace_database_urls');
?>
如果问题仍然存在,请考虑以下额外措施: