5

WordPress无需插件API实现文章发布后自动同步其他站点

 2 years ago
source link: https://www.huhexian.com/27336.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

WordPress无需插件API实现文章发布后自动同步其他站点

青山 2022-02-1713:30:41评论4066字

WordPress文章发布后自动同步文章到其他wordpress博客,不使用插件或者api,前些日子在贴吧看到一位小伙伴需要这个功能,现在将我的实现方法分享给大家。WordPress文章发布后同步文章到其他wordpress博客教程。

WordPress无需插件API实现文章发布后自动同步其他站点

思路是:在另一个 WordPress 站点创建一个API,文章发布时用cURL模拟 POST 请求 API 利用wp_insert_post()函数来创建文章。支持同步文章标题、内容、类型、分类、标签,分类需要另一个站点也有创建相同名称的分类,别名和ID不需要相同。

在负博客站点的根目录创建一个文件,命名为xxxxxxxx-post.php,代码如下:

并设置用于启动 API 的 key

  1. <?php
  2. 文章发表后同步到另一个站点(接收)
  3. define('WP_USE_THEMES', false);
  4. require_once("wp-load.php");
  5. $key='xxxxxxxxxx'; //设置API的密钥,建议设置复杂
  6. if($_POST['key']==$key){
  7. $categorys=explode(',',$_POST['category']);
  8. $category=array();
  9. for($x=1;$x<count($categorys);$x++) {
  10. $category[$x-1]=get_cat_ID($categorys[$x]);
  11. $info = array(
  12. 'post_title' => $_POST['title'],
  13. 'post_content' => $_POST['content'],
  14. 'post_status' => 'publish',
  15. 'post_author' => 1, //发布文章的作者ID,1 为管理员
  16. 'post_date' => $_POST['date'],
  17. 'tags_input' => $_POST['tags'],
  18. 'post_category' => $category,
  19. 'post_type' => $_POST['type']
  20. wp_insert_post( $info );
  21. ?>

在主博客主题的functions.php文件的最后一个?>前加入已下代码,并设置 key,修改 API 地址

  1. 文章发表后同步到另一个站点(发送)
  2. add_action('publish_post', 'E_sync_post'); //钩子,在文章发布时执行
  3. function E_sync_post($post_ID) {
  4. $key='xxxxxxxxxxxxx'; //输入你设置的密钥
  5. $url='http://xxxxxx/xxxxxxxxxxxx-post.php';//API地址(接受同步文章博客地址,例:xx表示为发布文章主博客,那填写API地址就是负博客地址)
  6. $post_info = get_post($post_ID);
  7. if ( $post_info->post_status == 'publish' && $_POST['original_post_status'] != 'publish' ) {
  8. $title=$_POST['post_title'];
  9. $content=$_POST['content'];
  10. $date=$_POST['aa'].'-'.$_POST['mm'].'-'.$_POST['jj'].' '.$_POST['hh'].':'.$_POST['mn'].':'.$_POST['ss'];
  11. $category='';
  12. for($x=1;$x<count($_POST['post_category']);$x++) {
  13. $category.=','.get_cat_name($_POST['post_category'][$x]);
  14. $type=$_POST['post_type'];
  15. $tags=str_replace('、',',',$_POST['tax_input']['post_tag']);
  16. if($_POST['newtag']['post_tag']){
  17. $tags.=','.str_replace('、',',',$_POST['newtag']['post_tag']);
  18. $data = 'key='.$key.'&title='.$title.'&content='.$content.'&date='.$date.'&category='.$category.'&type='.$type.'&tags='.$tags;
  19. $ch = curl_init (); //cURL模拟POST
  20. curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, TRUE );
  21. curl_setopt ( $ch, CURLOPT_POST, TRUE );
  22. curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
  23. curl_setopt ( $ch, CURLOPT_URL, $url );
  24. curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  25. $ret = curl_exec ( $ch );
  26. curl_close ( $ch );
  27. return $ret;

这样一来,在主站发表一篇文章后,镜像站点也就会发表出来一篇文章了,但也会有一些意外情况,比如不是马上发表出来,而是显示计划中,正常隔几分钟后会发表好,但也会有发表失败,需要在后台文章管理中,选择该发表失败文章,状态修改为已发布,更新即可。

一些意外情况的解决:

问题 1,由于主题升级后,functions.php 代码会被置换。用以上方法实现的内容镜像每次在主题升级后都需要修改 functions.php 代码,这会造成麻烦。 所以有如下解决办法,代码如下:

  1. <?php
  2. //文章推送
  3. add_action('publish_post', 'fanly_sync_post'); //钩子,在文章发布时执行
  4. function fanly_sync_post($post_ID) {
  5. $key='123456'; //输入你设置的密钥
  6. $url='http://6.3838521.com/post.php';//API 地址,就是接受数据的那个站点
  7. $post_info = get_post($post_ID);
  8. if ( $post_info->post_status == 'publish' && $_POST['original_post_status'] != 'publish' ) {
  9. $title=$_POST['post_title'];
  10. $content=$_POST['content'];
  11. $date=$_POST['aa'].'-'.$_POST['mm'].'-'.$_POST['jj'].' '.$_POST['hh'].':'.$_POST['mn'].':'.$_POST['ss'];
  12. $category='';
  13. for($x=1;$x<count($_POST['post_category']);$x++) {
  14. $category.=','.get_cat_name($_POST['post_category'][$x]);
  15. $type=$_POST['post_type'];
  16. $tags=str_replace('、',',',$_POST['tax_input']['post_tag']);
  17. if($_POST['newtag']['post_tag']){
  18. $tags.=','.str_replace('、',',',$_POST['newtag']['post_tag']);
  19. $data = 'key='.$key.'&title='.$title.'&content='.$content.'&date='.$date.'&category='.$category.'&type='.$type.'&tags='.$tags;
  20. $ch = curl_init (); //cURL 模拟 POST
  21. curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, TRUE );
  22. curl_setopt ( $ch, CURLOPT_POST, TRUE );
  23. curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
  24. curl_setopt ( $ch, CURLOPT_URL, $url );
  25. curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  26. $ret = curl_exec ( $ch );
  27. curl_close ( $ch );
  28. return $ret;
  29. ?>

复制上面的代码,最好是用 Notepad ++等工具另存为 php 文件,打包成 zip 文档,在 wordpress 插件安装后台上传,安装并启用。

这样就是一个插件形式存在了,主题升级后不再有影响。

问题 2,有些主题编辑器是支持密码可见付费可见等短代码的,但短代码在编辑模式跟输出模式是不一样的,到了镜像站的内容会是输出模式,有可能会输出异常。

我的解决办法也是采用小插件的办法,对这些代码进行一个自动修改。代码如下:

  1. <?php
  2. //内容文字替换
  3. function wpdaxue_replace_text($text){
  4. $replace = array(
  5. // '原始文字' => '替换为这些'
  6. '\"20\"]' => '"20"]',
  7. '\"10\"]' => '"10"]',
  8. '\"50\"]' => '"50"]'
  9. $text = str_replace(array_keys($replace), $replace, $text);
  10. return $text;
  11. add_filter('the_content', 'wpdaxue_replace_text'); //正文
  12. add_filter('the_excerpt', 'wpdaxue_replace_text'); //摘要
  13. add_filter('comment_text', 'wpdaxue_replace_text'); //评论
  14. ?>

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK