之前测试了款post-to-tiwtter的插件,发现完全不能使用,可能作者也放弃了更新还是怎么的,差不多一年左右没有更新了。但评分居然超高。又测试了一个类似的插件,但需要curl支持,可惜本空间供应商不支持。不想在闲逛之余,发现了已经有别人实现了不用插件的方法:http://www.clazh.com/post-wordpress-to-twitter-automatically-with-short-urls-no-plugin-required/
原来的内容是使用curl的,这个需要修改的,作者也提到了两种使用别的方法,我在本空间上测试了下,fsockopen和stream_context_creat均是支持的,那么就将curl修改为stream_context_creat吧,不用fsockopen是因为那个感觉更加的强大,强大意味着要敲更多的字,本着够用就行的原则,使用了stream_context_creat。就有了下面的内容:
','?','[',']','\\',';',"'",',','.','/','*','+','~','`','=');
$code_entities_replace = array('-','','','','','','','','','','','','','','','','','','','','','','','','');
$postTitle = str_replace($code_entities_match, $code_entities_replace, $postTitle);
// Check if New or Updated Post
//if($isNew)
$postTitle = '博客更新: ' . $postTitle;
//else
//$postTitle = 'Updated Post: ' . $postTitle;
// Calculate Twitter Msg and keep it under 140 Chars
if(strlen ($postTitle) > (140 - strlen ($postLink)))
$postTitle = substr_replace($postTitle, '...', (140 - 3 - strlen ($postLink)));
$message = $postTitle . $postLink;
// The twitter API address
$url = 'http://XXXX.appspot.com/statuses/update.xml';
/*
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "$url");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$message");
curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password");
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
*/
$context = stream_context_create(array( 'http' =>
array( 'method' => 'POST',
'header' => sprintf("Authorization: Basic %s\r\n", base64_encode($username.':'.$password))."Content-type: application/x-www-form-urlencoded\r\n",
'content' => http_build_query(array('status' => $message)),
'timeout' => 5, ),
));
$ret = file_get_contents($url, false, $context);
// Uncomment the lines below to check if
// CURL is enabled on your Web Server
// check for success or failure
/*
if (empty($buffer))
echo 'message';
else
echo 'success';
*/
}
function postToTwitter($post_ID)
{
// Create your Short URL replace with your blog url
$postLink = ' http://zeze0556.tk/?p=' . $post_ID;
// encode the URL to fix Post to Twitter issues
// $postLink = urlencode ( $postLink ) ;
// Get the Post Object
$get_post_info = get_post( $post_ID );
// Get the Post Title
$postTitle = $get_post_info->post_title;
// Get the Post date
$postDate = date('U', strtotime($get_post_info->post_date));
// Get the post Modified date
$postModified = date('U', strtotime($get_post_info->post_modified));
// Check if the post is new or modified
if($postModified == $postDate)
{
twitterUpdate($postTitle, $postLink, true);
}
/* If You want to fire everytime You update
a post remove these comments around the else.
else
{
twitterUpdate($postTitle, $postLink, false);
}
*/
}
// Post to twitter when you publish or update a post
add_action('publish_post', 'postToTwitter');
?>
使用时需要修改USER_NAME,PASSWD,XXXX等字样,如果你很幸运的话,空间提供商没有禁掉twitter的网址的话,你可以把那个twitter的url替换为官方的。不过我就没有那么lucky啦,于是就只能再次使用GTAP了。GTAP官方默认给的网址是https的,需要ssl支持的,可惜本空间供应商实在小气,不支持,于是修改了下GTAP的参数,使之接收http的访问,这下万事ok了。修改下GTAP的app.yaml的secure参数值为optional就可以了,不管是http还是https都支持的。
上面的代码放到主题的functions.php文件中即可。我只是将别人的代码简单修改了下,原始的代码不使用的部分被注释掉了,说不定有人比我幸运,直接使用curl就可以了。