PUBLISH_POST插件挂钩并不总是传递$POST->POST_CONTENT

时间:2013-01-30 作者:d13design

我编写了一个简单的插件,可以在帖子发布时获取帖子的内容,并将其发送到外部站点进行归档。这通常是可行的,但当处理以前没有保存或发布的帖子时,没有post_content 值可用。如果在发布之前另存为草稿,则所有操作都正常,只有在直接发布时,该值才为空。

我的插件是这样的:

function send_data($postID,$post){
  global $post;
  print_r($post); //used for debugging the contents of $post
  //stuff here to send the post for archiving
}
add_action(\'publish_post\',\'send_data\',2);
如果一切顺利,我将此作为$post的内容:

WP_Post Object ( [ID] => 159 [post_author] => 1 [post_date] => 2013-01-30 09:11:05 [post_date_gmt] => 0000-00-00 00:00:00 [post_content] => This is the content of the post [post_title] => Something new [post_excerpt] => [post_status] => draft [comment_status] => open [ping_status] => open [post_password] => [post_name] => [to_ping] => [pinged] => [post_modified] => 2013-01-30 09:11:05 [post_modified_gmt] => 2013-01-30 09:11:05 [post_content_filtered] => [post_parent] => 0 [guid] => http://wordpress/?p=159 [menu_order] => 0 [post_type] => post [post_mime_type] => [comment_count] => 0 [filter] => raw )
但是,如果我直接从创建帖子内容跳到“发布”按钮,我会得到一个空值$post->post_content:

WP_Post Object ( [ID] => 159 [post_author] => 1 [post_date] => 2013-01-30 09:11:05 [post_date_gmt] => 0000-00-00 00:00:00 [post_content] => [post_title] => Something new [post_excerpt] => [post_status] => draft [comment_status] => open [ping_status] => open [post_password] => [post_name] => [to_ping] => [pinged] => [post_modified] => 2013-01-30 09:11:05 [post_modified_gmt] => 2013-01-30 09:11:05 [post_content_filtered] => [post_parent] => 0 [guid] => http://wordpress/?p=159 [menu_order] => 0 [post_type] => post [post_mime_type] => [comment_count] => 0 [filter] => raw )
有没有人想过我该如何让这更可靠?有钩子吗after_publish_post 或者类似的东西?

任何想法都很感激。

1 个回复
SO网友:bueltge

不,这个要求没有挂钩。但是钩子save_post处理所有更改,您可以检查当前状态并仅执行此操作,状态值符合您的要求。

结束

相关推荐

多个单个站点,一个插件目录-调整plugins_url()

[这不是一个多站点问题]更新:这个问题在逻辑上是不可能的,或者是思维循环,如果你愿意的话,请检查下面的更新。技术是laid out here in WPEngineer.它允许有一个插件文件夹来为运行在同一服务器上的多个WordPress站点提供服务。所以,我所有的开发站点都在wp配置中有这个。phpdefine( \'WP_PLUGIN_DIR\', \'/www_dev/base-site/wp-content/plugins\' ); define( \'WP_PLUGIN_URL\', \