You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自建双WordPress站点内容同步镜像实现咨询(偏好PHP/HTML,禁用JS)

Absolutely feasible! Since you’ve got PHP/HTML fundamentals and want to avoid JavaScript, let’s walk through practical, server-side solutions that’ll get your WordPress page sync working—plus we’ll troubleshoot why your earlier cURL/file_get_contents attempts might have failed.

核心方案(纯PHP实现)

1. WordPress REST API + Post Hook(推荐,跨服务器友好)

WordPress自带的REST API是最规范的同步方式,能精准获取页面内容,还能触发自动同步当原页面更新时。

步骤1:获取原站点页面内容

解决你之前cURL失败的常见问题:原站点可能拦截无标识请求、有SSL验证限制,或者需要权限验证。下面是修正后的请求代码:

// 原站点目标页面的REST API端点(替换为你的页面ID和域名)
$source_page_id = 123;
$source_api_url = "https://your-source-site.com/wp-json/wp/v2/pages/{$source_page_id}";

$ch = curl_init($source_api_url);
// 关键配置:避免被安全插件/CDN拦截
curl_setopt($ch, CURLOPT_USERAGENT, 'WordPress Sync Bot/1.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 仅测试环境关闭SSL验证,生产环境请配置正确的CA证书
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$response = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);

if ($error) {
    error_log("Sync failed: " . $error);
    return;
}

// 解析返回的页面数据
$page_data = json_decode($response, true);
$sync_content = $page_data['content']['rendered']; // 已渲染的HTML内容

步骤2:同步到目标站点

用目标站点的REST API更新指定页面,需要开启应用密码(WordPress 5.6+支持,在用户后台设置):

$target_page_id = 456; // 目标站点要同步的页面ID
$target_api_url = "https://your-target-site.com/wp-json/wp/v2/pages/{$target_page_id}";
// 替换为目标站点的用户名和应用密码
$auth_token = base64_encode('target-username:your-app-password');

$update_payload = json_encode([
    'content' => $sync_content,
    'modified' => current_time('mysql')
]);

$ch = curl_init($target_api_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $update_payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    "Authorization: Basic {$auth_token}"
]);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$update_response = curl_exec($ch);
curl_close($ch);

步骤3:自动触发同步

在原站点的functions.php中添加钩子,当目标页面更新时自动执行同步:

add_action('save_post_page', 'auto_sync_to_target_site', 10, 3);
function auto_sync_to_target_site($post_id, $post, $update) {
    // 只处理指定的原页面,避免所有页面触发同步
    if ($post_id != 123 || !$update) return;
    
    // 把上面的获取+同步代码放到这里
}

2. 直接数据库同步(适合同服务器/可跨库访问场景)

如果两个站点在同一服务器,或者目标站点能访问原站点的数据库,这是效率最高的方式:

add_action('save_post_page', 'sync_via_database', 10, 3);
function sync_via_database($post_id, $post, $update) {
    if ($post_id != 123 || !$update) return;
    
    // 连接目标站点数据库(替换为你的目标库信息)
    $target_db = new wpdb(
        'target-db-username',
        'target-db-password',
        'target-db-name',
        'localhost'
    );
    $target_table_prefix = 'wp_'; // 目标站点的表前缀
    
    // 更新目标页面的内容和修改时间
    $target_db->update(
        "{$target_table_prefix}posts",
        [
            'post_content' => $post->post_content,
            'post_modified' => current_time('mysql'),
            'post_modified_gmt' => current_time('mysql', 1)
        ],
        ['ID' => 456] // 目标页面ID
    );
}

3. 自定义短代码+定时任务(适合手动/定期同步)

如果不需要实时同步,可在目标站点创建短代码,定期用Cron执行更新:

// 目标站点functions.php添加短代码
add_shortcode('synced_page_content', 'get_synced_content');
function get_synced_content() {
    // 把步骤1的获取内容代码放到这里,直接返回$sync_content
    return $sync_content;
}

然后在目标页面插入[synced_page_content],再用服务器Cron(比如Linux的crontab)定期请求该页面触发同步。

排查你之前cURL/file_get_contents失败的原因

  • 请求被拦截:原站点的安全插件(比如Wordfence)或CDN(比如Cloudflare)会拦截无User-Agent的请求,添加CURLOPT_USERAGENT即可解决。
  • SSL验证失败:原站点用了自签证书或SSL配置问题,临时关闭CURLOPT_SSL_VERIFYPEER(生产环境建议配置CA证书路径)。
  • 权限限制:如果原页面是私有页面,需要在请求头中携带登录Cookie或REST API授权令牌。
  • URL错误:确认原页面的REST API端点是否正确(可直接在浏览器访问测试)。

安全注意事项

  • 不要硬编码密码/令牌,用WordPress的update_option()存储敏感信息,get_option()读取。
  • 生产环境开启SSL验证,避免关闭CURLOPT_SSL_VERIFYPEER。
  • 限制同步钩子的触发范围,只针对需要同步的页面ID。

内容的提问来源于stack exchange,提问作者Zach

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:18:10