本文最后更新于2021年11月18日,已超过883天没有更新,若内容或图片失效,请留言反馈。

代码如下,拿去吧。记得把代码加到主题的functions.php里。

// 微博同步
    function post_to_sina_weibo($post_ID) {
      if( wp_is_post_revision($post_ID) ) return;
        $get_post_info = get_post($post_ID);
        $get_post_centent = get_post($post_ID)->post_content;
        //去掉文章内的html编码的空格、换行、tab等符号(如果你文章的编码格式是这样子,可以将下面的"//"去掉即开启此功能)
        //$get_post_centent = str_replace("\t", " ", str_replace("\n", " ", str_replace(" ", " ", $get_post_centent)));
        $get_post_title = get_post($post_ID)->post_title;
      if ( $get_post_info->post_status == 'publish' && $_POST['original_post_status'] != 'publish' ) {
        $request = new WP_Http;
        $status = '【' . strip_tags( $get_post_title ) . '】 ' . mb_strimwidth(strip_tags( apply_filters('the_content', $get_post_centent)),0, 132,'...') . ' 全文地址:' . get_permalink($post_ID) ;
        $api_url = 'https://api.weibo.com/2/statuses/update.json';
        $body = array( 'status' => $status, 'source'=>'xxx');
        $headers = array( 'Authorization' => 'Basic ' . 'xxx' );
     /*
     如果你使用该方法,请注释掉上面$headers = array( 'Authorization' => 'Basic ' . 'xxx' );
     换成如下代码
     //你的新浪微博登陆名
     $username = '' ;
     //你的新浪微博登陆密码
     $password = '' ;
     $headers = array( 'Authorization' => 'Basic ' .  base64_encode('$username:$password'));
     */
        $result = $request->post( $api_url , array( 'body' => $body, 'headers' => $headers ) );
        }
    }
    add_action('publish_post', 'post_to_sina_weibo', 0);

标签: none

评论已关闭