Twitpress の通知 URL にパーマリンクをちゃんと使いたい

2008年11月6日

WordPress 2.3 でカノニカル URL が導入されたのが原因か判断付かないのですが、TwitpressMessage format を、

更新したよ! : [title] [permalink]*Twitpress*

のようにしても、パーマリンクが通知されない状況が続いていました。別に、ちゃんとリダイレクトされるので問題ないなあと思ってたんですが、Twitter でそれに困ってるつぶやきを見かけたので、ソースを追ってみました。で、多分これで解決。

$ diff -u twitpress.php.org twitpress.php
--- twitpress.php.org   2008-10-26 10:29:45.000000000 +0900
+++ twitpress.php       2008-10-26 10:13:21.000000000 +0900
@@ -246,7 +246,8 @@
        $proto = get_option( 'twitpress_message' );
        $post = get_post( $postID );
        $proto = str_replace( "[title]", $post->post_title, $proto );
-       $proto = str_replace( "[permalink]", $post->guid, $proto );
+       //$proto = str_replace( "[permalink]", $post->guid, $proto );
+       $proto = str_replace( "[permalink]", get_permalink($postID), $proto );
        $proto = str_replace( "[link]", get_option( 'home' )."?p=".$postID, $proto );
        return $proto;
 }

[permalink] で置換されるURLはデータベース内の wp_posts テーブルの guid フィールドを参照しているのですが、これは更新したときもパーマリンクが格納されるのじゃなくて、カノニカルURL 用のパラメータが格納されている雰囲気。なので、その値を使わずに、get_permalinkを使い、パラメータに $postID を指定することで、投稿記事のパーマリンクを取得、投稿するようにしています。

まぁ、この記事がテスト投稿なんですが、ちゃんと通知されてますでしょうか?

ページを更新したときは通知したくない

twitpress.php の関数 twitpress_run を書き換えて、$post_typepost の時のみ通知を行うように変更してみました。

//Runs when a post record is inserted into the database
function twitpress_run( $postID ) {
	//get the post
	$post = get_post( $postID );

	//get the post type(page or post)
	$post_type = get_post_type( $postID );

	//we only want to do anything if the post was not previously twittered
	if ( !twitpress_was_twittered( $postID ) && ( $post_type === "post") ){
		//Update the post to reflect it's current status
		twitpress_db_update_post( $postID, $post->post_status );
	}
	//process the posts, including twittering newly published posts
	if ( $post_type === "post" ){
		twitpress_process_posts();
	}
}

ページを作成したときにTwitterへポストされるのがイヤな場合は、このような書き換えで回避できそうです。