我对wp在推特上发布一条推特,链接到我通过电子邮件发送到wordpress的一篇设置为“待定”的帖子有点不太满意。组织网站。电子邮件中有一个页脚,其中包括我的手机号码。
我决定创建自己的插件。
add_action(\'publish_post\', \'tcr_tweet\');
/* the function */
function tcr_tweet($postID)
{
if( ( $_POST[\'post_status\'] == \'publish\' ) && ( $_POST[\'original_post_status\'] != \'publish\' ) ) {
/* get the post that\'s being published */
$post = get_post($postID);
$post_title = $post->post_title;
/* get the author of the post */
$author_id=$post->post_author;
/* author needs a twitterid in their meta data*/
$author = get_the_author_meta(\'twitterid\',$author_id );
/* get the permalink and shorten it */
$url = get_permalink($postID);
$short_url = getBitlyUrl($url);
//check to make sure the tweet is within the 140 char limit
//if not, shorten and place ellipsis and leave room for link.
if (strlen($post_title) + strlen($short_url) > 100) {
$total_len = strlen($post_title) + strlen($short_url);
$over_flow_count = $total_len - 100;
$post_title = substr($post_title,0,strlen($post_title) - $over_flow_count - 3);
$post_title .= \'...\';
}
//add in the shortened bit.ly link
$message = "New: ".$post_title." - ".$short_url." by @".$author." #hashtag";
if ( $post->post_status != \'publish\' ) return;
//call the tweet function to tweet out the message
goTweet($message);
}
}
我的插件是一个简单的插件,它调用了我的位。ly函数,然后是另一个tweet函数,这些函数在其他地方使用,并且工作得很好。
我的问题是,如果我安排了一篇帖子,没有任何东西会被推特,如果我点击一篇新帖子上的发布,我会看到一个白色屏幕,但推特会被发送。
我如何才能正确定位预定的帖子?我看过我的$_POST
数据,看起来还可以。代码似乎很简单,所以我遗漏了一些东西。。谢谢
编辑:
我对WordPress如何处理排定的帖子有些困惑post_status=\'future\'
当时机成熟,这篇文章需要活起来的时候,它肯定会变成post_status=\'publish\'
因为它不再是“未来”的帖子。所以我的功能应该在
add_action(\'publish_post\', \'tcr_tweet\');
add_action(\'publish_future_post\', \'tcr_tweet\');
add_action(\'future_to_publish\', \'tcr_tweet\');
将触发这些操作。如果post\\U状态保持为“未来”,是否需要检查日期是否已过?
最合适的回答,由SO网友:Mat 整理而成
我修改了(添加了另一个版本的)我的函数,删除了if语句来检查帖子状态,因为帖子计划发布,所以我不需要再次检查它。
/* the function */
function tcr_tweet2($postID)
{
/* get the post that\'s being published */
$post = get_post($postID);
$post_title = $post->post_title;
/* get the author of the post */
$author_id=$post->post_author;
/* author needs a twitterid in their meta data*/
$author = get_the_author_meta(\'twitterid\',$author_id );
/* get the permalink and shorten it */
$url = get_permalink($postID);
$short_url = getBitlyUrl($url);
//check to make sure the tweet is within the 140 char limit
//if not, shorten and place ellipsis and leave room for link.
if (strlen($post_title) + strlen($short_url) > 100) {
$total_len = strlen($post_title) + strlen($short_url);
$over_flow_count = $total_len - 100;
$post_title = substr($post_title,0,strlen($post_title) - $over_flow_count - 3);
$post_title .= \'...\';
}
//add in the shortened bit.ly link
$message = "New: ".$post_title." - ".$short_url." by @".$author." #hashtag";
if ( $post->post_status != \'publish\' ) return;
//call the tweet function to tweet out the message
goTweet($message);
}
然后,我可以为这个版本使用下面的钩子,它可以正常工作。
add_action(\'future_to_publish\', \'tcr_tweet2\');