You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
/**
* WordPress站点URL修复脚本
* 用于在数据库迁移后更新站点URL配置
*/
// 加载WordPress环境
require_once __DIR__ . '/wp-load.php';
// 获取当前配置的本地URL
$local_url = 'http://localhost:82';
echo "=== WordPress站点URL修复工具 ===\n\n";
// 1. 获取当前数据库中的siteurl和home值
$current_siteurl = get_option('siteurl');
$current_home = get_option('home');
echo "当前数据库中的配置:\n";
echo "siteurl: " . $current_siteurl . "\n";
echo "home: " . $current_home . "\n\n";
// 2. 更新为本地URL
echo "正在更新为本地URL: " . $local_url . "\n\n";
update_option('siteurl', $local_url);
update_option('home', $local_url);
// 3. 验证更新结果
$new_siteurl = get_option('siteurl');
$new_home = get_option('home');
echo "更新后的配置:\n";
echo "siteurl: " . $new_siteurl . "\n";
echo "home: " . $new_home . "\n\n";
// 4. 清除缓存
if (function_exists('wp_cache_flush')) {
wp_cache_flush();
echo "缓存已清除\n\n";
}
// 5. 检查是否需要修复其他URL
echo "检查是否需要修复其他URL...\n";
// 修复文章内容中的URL可选
global $wpdb;
$old_url = $current_siteurl;
$count = $wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->posts}
SET post_content = REPLACE(post_content, %s, %s)",
$old_url,
$local_url
)
);
if ($count > 0) {
echo "已修复 {$count} 篇文章中的URL链接\n\n";
} else {
echo "文章内容URL无需修复\n\n";
}
// 6. 修复选项表中的其他URL
$options_to_fix = [
'upload_path',
'upload_url_path',
'widget_text',
'sidebars_widgets',
];
foreach ($options_to_fix as $option_name) {
$option_value = get_option($option_name);
if ($option_value && is_string($option_value) && strpos($option_value, $old_url) !== false) {
$new_value = str_replace($old_url, $local_url, $option_value);
update_option($option_name, $new_value);
echo "已修复选项: {$option_name}\n";
}
}
echo "\n=== 修复完成! ===\n";
echo "现在可以访问: http://localhost:82wp-admin\n";
echo "请使用原有的管理员账号登录\n";