三月 29 2007
修改 Twitter Update
先前有提到 WordPress 的一個 Twitter Plugin -- Twitter Update。實際在 2.1.2 使用時,發現了幾個奇怪的地方。首先是如果你勾選了「Update Twitter when the an old post has been edited」,理論上應該在修改舊文章時才會發出 Tweets,但是實際使用時卻會發現文章尚未發佈,卻持續送出更新訊息。另外即使勾選了「Update Twitter when the new post is published」,發佈新文章時,卻反而不會送出 Tweets。
稍微研究了一下 twitter_update.php 這隻程式,發覺它判斷新文章與否,是以文章的前一個狀態是否為草稿作為依據,也就是:
-
if ($_POST['prev_status'] == 'draft') {
-
// is new post
-
} else {
-
// is old post
-
}
但是 WordPress 2.1 之後,多了 Autosave 的功能,在你編輯草稿時,系統會在背後幫你做自動儲存。而編輯草稿透過 Autosave 儲存時,一樣也會觸發 Twitter Update,但此時文章的 prev_status 卻是空值,因此會造成程式判斷該文章為舊文章。所以先前才會發生我文章還沒發佈,卻送出舊文章修改的 Tweets 問題。
最簡單的方式,就是修改判斷式。首先在 twitter_update.php 中找到 function vc_twit,然後將
-
if ($_POST['prev_status'] == 'draft') {
取代為
-
if ($_POST['prev_status'] != 'publish') {
即可。
另外,在判斷是否發佈的地方,原程式是寫做
-
if ($_POST['publish'] == 'publish') {
這個寫法在非英語系的 WordPress 都會有問題。在中文化的 WordPress 中,發佈文章後,$_POST['publish'] 傳回來的會是中文的「發表」,將會造成程式判斷為尚在修改新文章中,而送出「Update Twitter when the new post is edited (re-saved but not published)」選項中設定的訊息。
修改的方式,是將上面那段程式碼修改為:
修改過後 vc_twit function 的完整程式碼如下:
-
function vc_twit($post_ID) {
-
$twitterURI = "/statuses/update.xml";
-
$thisposttitle = $_POST['post_title'];
-
$thispostlink = get_permalink($post_ID);
-
$sentence = '';
-
-
//is new post
-
if($_POST['prev_status'] != 'publish'){
-
-
// publish new post
-
if(get_option('newpost-published-update') == '1'){
-
$sentence = get_option('newpost-published-text');
-
if(get_option('newpost-published-showlink') == '1'){
-
$thisposttitle = $thisposttitle . ' ( ' . $thispostlink . ' )';
-
}
-
}
-
}else if($_POST['action'] == 'post'){
-
// create new post
-
if(get_option('newpost-created-update') == '1'){
-
$sentence = get_option('newpost-created-text');
-
}
-
}else{
-
// edit new post
-
if(get_option('newpost-edited-update') == '1'){
-
$sentence = get_option('newpost-edited-text');
-
}
-
}
-
}else{
-
// is old post
-
if(get_option('oldpost-edited-update') == '1'){
-
$sentence = get_option('oldpost-edited-text');
-
if(get_option('oldpost-edited-showlink') == '1'){
-
$thisposttitle = $thisposttitle . ' ( ' . $thispostlink . ' )';
-
}
-
}
-
}
-
-
if($sentence != ''){
-
$sendToTwitter = vc_doTwitterAPIPost('status='.$sentence, $twitterURI);
-
}
-
-
return $post_ID;
-
}
相關文章
2 responses so far




升級到2.2版本,也按你所謂而修改draft 為publish,但依然不行,是因為dreamhost的關係嗎
我也是使用 Dreamhost,
上面有兩個修改喔,
不只是把 draft 改成 publish 而已。