過去ログツイートのテスト

  • タグ:
  • タグはありません
require_once 'tmhOAuth.php';
function tweetPost(){

if($_GET['tweet'] == 1){ //追加:http://example.com?tweet=1 とアクセスした時に実行するように

    //トークンなどの設定
    $tmhOAuth  = new tmhOAuth(array(
        "consumer_key" => 'カスタマーキー',
        "consumer_secret" => 'カスタマーシークレット',
        "user_token" => 'アクセストークン',
        "user_secret" => 'アクセストークンシークレット',
        'curl_ssl_verifypeer' => false //SSL対応に必要
    ));
     
    //ツイートする記事の取得
    //いつものget_postsなので、ここでツイートしたい記事の条件をいろいろ指定できる。
    $post = get_posts(array(
        'numberposts' => 1,
        'orderby' => 'rand'
    ));
     
    //ツイートするテキスト(字数は定型文やハッシュタグを使う場合は適宜調整が必要)
    //以下の例は本文とURLをツイートするものですが、もちろんpost_titleも可です。
    $params = array(
        'status' => mb_strimwidth('【過去記事】'.strip_tags($post[0]->post_content), 0, 116, "...") . ' ' . get_permalink($post[0]->ID)
    );
     
    //ツイートする処理
    $code = $tmhOAuth->request(
        'POST',
        'https://api.twitter.com/1.1/statuses/update.json',
        $params,
        true,
        true
    );
     
     
    /*アイキャッチを添付画像にしてツイートしたい場合は以下のようになります
        $file = wp_get_attachment_image_src( get_post_thumbnail_id($post[0]->ID), 'full' );
        $file = $file[0];
        $image = file_get_contents( $file );
        $imagesize = getimagesize( $file );
 
        $params = array(
            'status' =>  mb_strimwidth('【過去記事】'.strip_tags($post[0]->post_content), 0, 116, "...") . ' ' . get_permalink($post[0]->ID),
            'media[]' => $image . ";type=" . $imagesize['mime'] . ";filename=" . basename( $file )
        );
         
        //URLが違うことに注意
        $code = $tmhOAuth->request(
            'POST',
            'https://api.twitter.com/1.1/statuses/update_with_media.json',
            $params,
            true,
            true
        );
    */
} //追加:if文の終わり
}

add_action('wp_head','tweetPost');  //追加:header読み込み時に上記アクションを実行(initだとGETが取れない)