在运营多个 WordPress 网站时,常常会遇到一个需求:主站发布文章后,希望自动同步到子站点或其他博客上。本文将教你如何通过简单的 PHP 代码,实现 WordPress 文章自动推送到另一个站点,同时还能进行文章内容自动替换,实现多站点高效运维。
🧠 本教程适合谁?
- 同时运营多个 WordPress 网站的站长
- 想在发布主站文章时自动同步到其他站点
- 需要自动替换特定关键词或敏感词的用户
- 了解 PHP 和 WordPress 基础操作
一、功能总览
我们将实现如下功能:
- ✅ 主站文章发布后,自动发送数据到子站点
- ✅ 子站点接收数据后直接发布为新文章
- ✅ 可设置分类、标签、发布时间、文章类型等信息
- ✅ 可配置文字内容的自动替换(例如敏感词过滤)
二、核心结构与说明
我们将使用两段代码:
✅【1】发送端(主站)
主站每次发布文章时,将信息通过 curl
发送到子站点的 API 接口。
<?php
add_action('publish_post', 'E_sync_post');
function E_sync_post($post_ID) {
$key = 'xxxxxxxxxxxxx';
$url = 'http://副站.com/receive-post.php';
$post_info = get_post($post_ID);
if ($post_info->post_status == 'publish' && $_POST['original_post_status'] != 'publish') {
$title = $_POST['post_title'];
$content = $_POST['content'];
$date = $_POST['aa'].'-'.$_POST['mm'].'-'.$_POST['jj'].' '.$_POST['hh'].':'.$_POST['mn'].':'.$_POST['ss'];
$category = '';
for ($x = 1; $x < count($_POST['post_category']); $x++) {
$category .= ',' . get_cat_name($_POST['post_category'][$x]);
}
$type = $_POST['post_type'];
$tags = str_replace('、', ',', $_POST['tax_input']['post_tag']);
if ($_POST['newtag']['post_tag']) {
$tags .= ',' . str_replace('、', ',', $_POST['newtag']['post_tag']);
}
$data = http_build_query([
'key' => $key,
'title' => $title,
'content' => $content,
'date' => $date,
'category' => $category,
'type' => $type,
'tags' => $tags
]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_exec($ch);
curl_close($ch);
}
}
?>
✅【2】接收端(子站)
接收数据并发布为新文章。
<?php
define('WP_USE_THEMES', false);
require_once("wp-load.php");
$key = 'xxxxxxxxxx';
if ($_POST['key'] == $key) {
$category_names = explode(',', $_POST['category']);
$categories = [];
for ($x = 1; $x < count($category_names); $x++) {
$categories[] = get_cat_ID($category_names[$x]);
}
$info = [
'post_title' => $_POST['title'],
'post_content' => $_POST['content'],
'post_status' => 'publish',
'post_author' => 1,
'post_date' => $_POST['date'],
'tags_input' => $_POST['tags'],
'post_category' => $categories,
'post_type' => $_POST['type']
];
wp_insert_post($info);
}
?>
三、文字内容自动替换(可选功能)
如需在同步内容时自动替换指定文本(如过滤敏感词),可以启用以下功能:
<?php
function wpdaxue_replace_text($text) {
$replace = array(
'百度' => '某搜索',
'微信' => '某信'
);
return str_replace(array_keys($replace), $replace, $text);
}
add_filter('the_content', 'wpdaxue_replace_text');
add_filter('the_excerpt', 'wpdaxue_replace_text');
add_filter('comment_text', 'wpdaxue_replace_text');
?>
四、安全性建议
- 设置复杂密钥:防止接口被非法访问
- 启用 HTTPS:防止内容在传输过程中被窃取
- 限制请求 IP:加强 API 的访问控制
- 加入超时机制:可进阶扩展,防止接口长时间暴露
五、常见问题解决
- 分类无法对应:确保主副站分类一致
- 中文乱码:检查数据库编码与页面编码是否一致
- curl 无响应:检查目标站是否允许外部 POST
六、实际应用场景
- 博客自动在内容聚合站同步发布
- 主站内容镜像到备用站点
- 多语言 WordPress 同步不同语言的文章
七、总结
以上代码和逻辑可帮助你实现文章自动同步发布到另一个 WordPress 站点的功能,不再需要手动复制粘贴,同时还支持关键词替换,增强内容安全与合规性。
📌 如果你希望把多个站点组成一个“文章联盟”或自动化网络,这个方法简单、稳定、可扩展,非常值得尝试!
想要将其制作成插件或页面模板?欢迎访问 www.iqsoo.com 获取更多资源。
评论前必须登录!
立即登录 注册