WordPressの過去記事を自動ツイートを設定できるプラグイン
Twitterで過去記事を自動で呟く仕組みいいなぁ。。と。
で、そのやり方も@mk_mizuhoさんの自動ツイートで知った次第です。
PickUp!! ➔: [Å] WordPressプラグイン「Tweetily」を使って過去記事をランダムにツイートするようにしてみた! http://t.co/ifLegllkCc
— あかめ (@mk_mizuho) September 23, 2013
[Å] WordPressプラグイン「Tweetily」を使って
過去記事をランダムにツイートするようにしてみた!
Tweetily
Tweetilyをまずダウンロード。直接「プラグインの新規追加」でもOK
で、有効化したら注意点。あかめさんのブログにもあるけど
日本語未対応!
なので、ちょっとコアファイルの修正が必要です。
日本語に対応する修正方法
①プラグイン編集から②で「Tweetily」を選択して「tweetily-tweet-wordpress-posts-automatically/top-core.php」をクリック。
で、これを変更するコードがあかめさんの記事に書いてあり、
古いかもしれないから気をつけろよ!
と書いてあるのを全開で無視してバックアップを取らず盲目的にコピーペースト!
⇒「ファイルを保存」!
エラー!!!!!
いう事は聞かないとダメですね。気を取り直して、再インストール。
下記コード作成して全部コピペ(2013.09.24現在)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 |
<!--?php require_once( 'Include/top-oauth.php' ); global $top_oauth; $top_oauth = new TOPOAuth; if ( function_exists('w3tc_pgcache_flush') ) { w3tc_pgcache_flush(); w3tc_dbcache_flush(); w3tc_minify_flush(); w3tc_objectcache_flush(); $cache = ' and W3TC Caches cleared'; } function top_tweet_old_post() { //check last tweet time against set interval and span if (top_opt_update_time()) { update_option('top_opt_last_update', time()); top_opt_tweet_old_post(); } } function top_currentPageURL() { if(!isset($_SERVER['REQUEST_URI'])){ $serverrequri = $_SERVER['PHP_SELF']; }else{ $serverrequri = $_SERVER['REQUEST_URI']; } $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : ""; $protocol = top_strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s; $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]); return $protocol."://".$_SERVER['SERVER_NAME'].$port.$serverrequri; } function top_strleft($s1, $s2) { return mb_substr($s1, 0, strpos($s1, $s2)); } //get random post and tweet function top_opt_tweet_old_post() { return top_generate_query(); } function top_generate_query($can_requery = true) { global $wpdb; $rtrn_msg=""; $omitCats = get_option('top_opt_omit_cats'); $maxAgeLimit = get_option('top_opt_max_age_limit'); $ageLimit = get_option('top_opt_age_limit'); $exposts = get_option('top_opt_excluded_post'); $exposts = preg_replace('/,,+/', ',', $exposts); $top_opt_tweeted_posts = array(); $top_opt_tweeted_posts = get_option('top_opt_tweeted_posts'); if(!$top_opt_tweeted_posts) $top_opt_tweeted_posts = array(); if($top_opt_tweeted_posts != null) $already_tweeted = implode(",", $top_opt_tweeted_posts); else $already_tweeted=""; if (mb_substr($exposts, 0, 1) == ",") { $exposts = mb_substr($exposts, 1, mb_strlen($exposts)); } if (mb_substr($exposts, -1, 1) == ",") { $exposts = mb_substr($exposts, 0, mb_strlen($exposts) - 1); } if (!(isset($ageLimit) && is_numeric($ageLimit))) { $ageLimit = top_opt_AGE_LIMIT; } if (!(isset($maxAgeLimit) && is_numeric($maxAgeLimit))) { $maxAgeLimit = top_opt_MAX_AGE_LIMIT; } if (!isset($omitCats)) { $omitCats = top_opt_OMIT_CATS; } $as_post_type = get_option('as_post_type'); $as_number_tweet = get_option('as_number_tweet'); if($as_number_tweet<=0){$as_number_tweet = 1;} if($as_number_tweet-->10){$as_number_tweet = 10;} //trying to fix multiposts //if($last<1){$as_number_tweet = 0;} $pt=''; if($as_post_type!='all'){ $pt = "post_type = '$as_post_type' AND"; } $sql = "SELECT ID FROM $wpdb->posts WHERE $pt post_status = 'publish' "; if(is_numeric($ageLimit)) { if($ageLimit > 0) $sql = $sql . " AND post_date <= curdate( ) - INTERVAL " . $ageLimit . " day"; } if ($maxAgeLimit != 0) { $sql = $sql . " AND post_date >= curdate( ) - INTERVAL " . $maxAgeLimit . " day"; } if (isset($exposts)) { if (trim($exposts) != '') { $sql = $sql . " AND ID Not IN (" . $exposts . ") "; } } if (isset($already_tweeted)) { if(trim($already_tweeted) !="") { $sql = $sql . " AND ID Not IN (" . $already_tweeted . ") "; } } if ($omitCats != '') { $sql = $sql . " AND NOT (ID IN (SELECT tr.object_id FROM " . $wpdb->prefix . "term_relationships AS tr INNER JOIN " . $wpdb->prefix . "term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy = 'category' AND tt.term_id IN (" . $omitCats . ")))"; } $sql = $sql . " ORDER BY RAND() LIMIT $as_number_tweet "; $oldest_post = $wpdb->get_results($sql); if($oldest_post == null) { if($can_requery) { $top_opt_tweeted_posts=array(); update_option('top_opt_tweeted_posts', $top_opt_tweeted_posts); return top_generate_query(false); } else { return "No post found to tweet. Please check your settings and try again.--".$sql; } } if(isset($oldest_post)){ $ret = ''; foreach($oldest_post as $k=>$odp){ array_push($top_opt_tweeted_posts, $odp->ID); $ret .= 'Tweet '.$k.' : '.top_opt_tweet_post($odp->ID).'<br />'; } update_option('top_opt_tweeted_posts', $top_opt_tweeted_posts); if ( function_exists('w3tc_pgcache_flush') ) { w3tc_pgcache_flush(); w3tc_dbcache_flush(); w3tc_minify_flush(); w3tc_objectcache_flush(); $cache = ' and W3TC Caches cleared'; } $next_tweet_time = time()+ get_option('top_opt_interval') * 60 * 60; update_option('next_tweet_time', $next_tweet_time); return $ret; } return $rtrn_msg; } //tweet for the passed random post function top_opt_tweet_post($oldest_post) { global $wpdb; $post = get_post($oldest_post); $content = ""; $to_short_url = true; $shorturl = ""; $tweet_type = get_option('top_opt_tweet_type'); $additional_text = get_option('top_opt_add_text'); $additional_text_at = get_option('top_opt_add_text_at'); $include_link = get_option('top_opt_include_link'); $custom_hashtag_option = get_option('top_opt_custom_hashtag_option'); $custom_hashtag_field = get_option('top_opt_custom_hashtag_field'); $twitter_hashtags = get_option('top_opt_hashtags'); $url_shortener = get_option('top_opt_url_shortener'); $custom_url_option = get_option('top_opt_custom_url_option'); $to_short_url = get_option('top_opt_use_url_shortner'); $use_inline_hashtags = get_option('top_opt_use_inline_hashtags'); $hashtag_length = get_option('top_opt_hashtag_length'); if ($include_link != "false") { $permalink = get_permalink($oldest_post); if ($custom_url_option) { $custom_url_field = get_option('top_opt_custom_url_field'); if (trim($custom_url_field) != "") { $permalink = trim(get_post_meta($post->ID, $custom_url_field, true)); } } if ($to_short_url) { if ($url_shortener == "bit.ly") { $bitly_key = get_option('top_opt_bitly_key'); $bitly_user = get_option('top_opt_bitly_user'); $shorturl = shorten_url($permalink, $url_shortener, $bitly_key, $bitly_user); } else { $shorturl = shorten_url($permalink, $url_shortener); } } else { $shorturl = $permalink; } } if ($tweet_type == "title" || $tweet_type == "titlenbody") { $title = stripslashes($post->post_title); $title = strip_tags($title); $title = preg_replace('/\s\s+/', ' ', $title); } else { $title = ""; } if ($tweet_type == "body" || $tweet_type == "titlenbody") { $body = stripslashes($post->post_content); $body = strip_tags($body); $body = preg_replace('/\s\s+/', ' ', $body); } else { $body = ""; } if ($tweet_type == "titlenbody") { if ($title == null) { $content = $body; } elseif ($body == null) { $content = $title; } else { $content = $title . " - " . $body; } } elseif ($tweet_type == "title") { $content = $title; } elseif ($tweet_type == "body") { $content = $body; } if ($additional_text != "") { if ($additional_text_at == "end") { $content = $content . " - " . $additional_text; } elseif ($additional_text_at == "beginning") { $content = $additional_text . ": " . $content; } } $hashtags = ""; $newcontent = ""; if ($custom_hashtag_option != "nohashtag") { if ($custom_hashtag_option == "common") { //common hashtag $hashtags = $twitter_hashtags; } //post custom field hashtag elseif ($custom_hashtag_option == "custom") { if (trim($custom_hashtag_field) != "") { $hashtags = trim(get_post_meta($post->ID, $custom_hashtag_field, true)); } } elseif ($custom_hashtag_option == "categories") { $post_categories = get_the_category($post->ID); if ($post_categories) { foreach ($post_categories as $category) { $tagname = str_replace(".", "", str_replace(" ", "", $category->cat_name)); if ($use_inline_hashtags) { if (mb_strrpos($content, $tagname) === false) { $hashtags = $hashtags . "#" . $tagname . " "; } else { $newcontent = preg_replace('/\b' . $tagname . '\b/i', "#" . $tagname, $content, 1); } } else { $hashtags = $hashtags . "#" . $tagname . " "; } } } } elseif ($custom_hashtag_option == "tags") { $post_tags = get_the_tags($post->ID); if ($post_tags) { foreach ($post_tags as $tag) { $tagname = str_replace(".", "", str_replace(" ", "", $tag->name)); if ($use_inline_hashtags) { if (mb_strrpos($content, $tagname) === false) { $hashtags = $hashtags . "#" . $tagname . " "; } else { $newcontent = preg_replace('/\b' . $tagname . '\b/i', "#" . $tagname, $content, 1); } } else { $hashtags = $hashtags . "#" . $tagname . " "; } } } } if ($newcontent != "") $content = $newcontent; } if ($include_link != "false") { if (!is_numeric($shorturl) && (strncmp($shorturl, "http", mb_strlen("http")) == 0)) { } else { return "OOPS!!! problem with your URL shortning service. Some signs of error " . $shorturl . "."; } } $message = set_tweet_length($content, $shorturl, $hashtags, $hashtag_length); $status = urlencode(stripslashes(urldecode($message))); if ($status) { $poststatus = top_update_status($message); if ($poststatus == true) { return "Success! Just tweeted successfully."; } else { return "Oh no! Something went wrong. Please try again."; } } return "Oh no! Looks like there are problems. Please email flavio@themana.gr."; } //send request to passed url and return the response function send_request($url, $method='GET', $data='', $auth_user='', $auth_pass='') { $ch = curl_init($url); if (strtoupper($method) == "POST") { curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } if (ini_get('open_basedir') == '' && ini_get('safe_mode') == 'Off') { curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); } curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); if ($auth_user != '' && $auth_pass != '') { curl_setopt($ch, CURLOPT_USERPWD, "{$auth_user}:{$auth_pass}"); } $response = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpcode != 200) { return $httpcode; } return $response; } /* returns a result form url */ function top_curl_get_result($url) { $ch = curl_init(); $timeout = 5; curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); $data = curl_exec($ch); curl_close($ch); return $data; } function top_get_bitly_short_url($url,$login,$appkey,$format='txt') { $connectURL = 'http://api.bit.ly/v3/shorten?login='.$login.'&apiKey='.$appkey.'&uri='.urlencode($url).'&format='.$format; return top_curl_get_result($connectURL); } //Shorten long URLs with is.gd or bit.ly. function shorten_url($the_url, $shortener='is.gd', $api_key='', $user='') { if (($shortener == "bit.ly") && isset($api_key) && isset($user)) { $response = top_get_bitly_short_url($the_url, $user, $api_key); } elseif ($shortener == "su.pr") { $url = "http://su.pr/api/simpleshorten?url={$the_url}"; $response = send_request($url, 'GET'); } elseif ($shortener == "tr.im") { $url = "http://api.tr.im/api/trim_simple?url={$the_url}"; $response = send_request($url, 'GET'); } elseif ($shortener == "3.ly") { $url = "http://3.ly/?api=em5893833&u={$the_url}"; $response = send_request($url, 'GET'); } elseif ($shortener == "tinyurl") { $url = "http://tinyurl.com/api-create.php?url={$the_url}"; $response = send_request($url, 'GET'); } elseif ($shortener == "u.nu") { $url = "http://u.nu/unu-api-simple?url={$the_url}"; $response = send_request($url, 'GET'); } elseif ($shortener == "1click.at") { $url = "http://1click.at/api.php?action=shorturl&url={$the_url}&format=simple"; $response = send_request($url, 'GET'); } else { $url = "http://is.gd/api.php?longurl={$the_url}"; $response = send_request($url, 'GET'); } return $response; } //Shrink a tweet and accompanying URL down to 140 chars. function set_tweet_length($message, $url, $twitter_hashtags="", $hashtag_length=0) { $tags = $twitter_hashtags; $message_length = mb_strlen($message); $url_length = mb_strlen($url); //$cur_length = mb_strlen($tags); if ($hashtag_length == 0) $hashtag_length = mb_strlen($tags); if ($twitter_hashtags != "") { if (mb_strlen($tags) > $hashtag_length) { $tags = mb_substr($tags, 0, $hashtag_length); $tags = mb_substr($tags, 0, mb_strrpos($tags, ' ')); } $hashtag_length = mb_strlen($tags); } if ($message_length + $url_length + $hashtag_length > 140) { $shorten_message_to = 140 - $url_length - $hashtag_length; $shorten_message_to = $shorten_message_to - 4; //$message = $message." "; if (mb_strlen($message) > $shorten_message_to) { $message = mb_substr($message, 0, $shorten_message_to); $message = mb_substr($message, 0, mb_strrpos($message, ' ')); } $message = $message . "..."; } return $message . " " . $url . " " . $tags; } //check time and update the last tweet time function top_opt_update_time() { return top_to_update(); } if ( function_exists('w3tc_pgcache_flush') ) { w3tc_pgcache_flush(); w3tc_dbcache_flush(); w3tc_minify_flush(); w3tc_objectcache_flush(); $cache = ' and W3TC Caches cleared'; } function top_to_update() { global $wpdb; //have to use normal query to prevent the caching plug-in from caching the last update time $last = $wpdb->get_var("select SQL_NO_CACHE option_value from $wpdb->options where option_name = 'top_opt_last_update';"); //$last_test = get_option('top_opt_last_update'); $interval = get_option('top_opt_interval'); $slop = get_option('top_opt_interval_slop'); if (!(isset($interval))) { $interval = top_opt_INTERVAL; } else if(!(is_numeric($interval))) { $interval = top_opt_INTERVAL; } if (!(isset($slop))) { $slop = top_opt_INTERVAL_SLOP; } else if(!(is_numeric($slop))) { $slop = top_opt_INTERVAL_SLOP; } $passed = time() - $last; //old testing code to test the actual values going into the DB //$wpdb->query("insert into wp_timetable (time,last,ret,url) values('".time()."',$last,$passed,'".$_SERVER['PHP_SELF']."');"); $interval = $interval * 60 * 60; $slop = $slop * 60 * 60; if (false === $last) { $ret = 1; } else if (is_numeric($last)) { $ret = (time() - $last) > $interval; } return $ret; } function top_get_auth_url() { global $top_oauth; $settings = top_get_settings(); $token = $top_oauth->get_request_token(); if ($token) { $settings['oauth_request_token'] = $token['oauth_token']; $settings['oauth_request_token_secret'] = $token['oauth_token_secret']; top_save_settings($settings); return $top_oauth->get_auth_url($token['oauth_token']); } } function top_update_status($new_status) { global $top_oauth; $settings = top_get_settings(); if (isset($settings['oauth_access_token']) && isset($settings['oauth_access_token_secret'])) { return $top_oauth->update_status($settings['oauth_access_token'], $settings['oauth_access_token_secret'], $new_status); } return false; } function top_has_tokens() { $settings = top_get_settings(); return ( $settings['oauth_access_token'] && $settings['oauth_access_token_secret'] ); } function top_is_valid() { return twit_has_tokens(); } function top_do_tweet($post_id) { $settings = top_get_settings(); $message = top_get_message($post_id); // If we have a valid message, Tweet it // this will fail if the Tiny URL service is done if ($message) { // If we successfully posted this to Twitter, then we can remove it from the queue eventually if (twit_update_status($message)) { return true; } } return false; } function top_get_settings() { global $top_defaults; $settings = $top_defaults; $wordpress_settings = get_option('top_settings'); if ($wordpress_settings) { foreach ($wordpress_settings as $key => $value) { $settings[$key] = $value; } } return $settings; } function top_save_settings($settings) { update_option('top_settings', $settings); } function top_reset_settings() { delete_option('top_settings'); update_option('top_enable_log',''); update_option('top_opt_add_text',''); update_option('top_opt_add_text_at','beginning'); update_option('top_opt_age_limit',0); update_option('top_opt_bitly_key',''); update_option('top_opt_bitly_user',''); update_option('top_opt_custom_hashtag_field',''); update_option('top_opt_custom_hashtag_option','nohashtag'); update_option('top_opt_custom_url_field',''); update_option('top_opt_custom_url_option',''); update_option('top_opt_excluded_post',''); update_option('top_opt_hashtags',''); update_option('top_opt_hashtag_length','20'); update_option('top_opt_include_link','no'); update_option('top_opt_interval',4); update_option('top_opt_interval_slop',4); delete_option('top_opt_last_update'); update_option('top_opt_max_age_limit',0); update_option('top_opt_omit_cats',''); update_option('top_opt_tweet_type','title'); delete_option('top_opt_tweeted_posts'); update_option('top_opt_url_shortener','is.gd'); update_option('top_opt_use_inline_hashtags',''); update_option('top_opt_use_url_shortner',''); } ?> |
※要バックアップ。自己責任で(今回もそうですがプラグインのアップデートで不具合出る可能性あり。都度、再修正)
要は「strlen」と「substr」と「strrpos」を「mb_strlen」、「mb_substr」、「mb_strrpos」に修正しろよって事です。
Twitterとの連携
まずは自分のTwitterアカウントにログインしておきます。
②の「Sign with Twitter」をクリック
連携アプリを認証
以上!
超簡単・・・他のツイートアプリもっと面倒なのに。
Twittilyの基本設定
①でツイートタイトルの前の文字。ちゃんと「過去記事」というのは分かるようにしておいた方が良いと思います。②が「リンク」の追加!(これないと意味ない)。。で、③が呟く間隔。多いとウザイので20時間(1日1回くらい)にしてみました。
その他は、見ればわかると思いますので適宜変更を。
最後に「Updatete Tweetily Options」を押せば完了ですね。
「Tweet Now!」を押せばすぐにツイートして動作確認できます。
[過去記事] : ゴメンね!こんな人はフォローしたくない。。きちんとフォロワーを増やす方法 http://t.co/5wrb06TSEo
— ENJI (@ENJILOG) September 24, 2013
ぜひ、皆さんも!