publish_post plugin hook doesn’t always pass $post->post_content

I’ve written a simple plugin that grabs the contents of a post, as the post is published, and sends them to an external site for archiving. This generally works but when dealing with posts that haven’t previously been saved or published, no post_content value is available. If you save as draft before publishing it all works fine, it’s only when you publish straight away that the value is empty.

My plugin is something like this:

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);

And, if all goes well, I get this as the content of $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 )

But, if I jump straight from creating my post contents to the ‘publish’ button, I get an empty value for $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 )

Does anyone have any thoughts on how I can make this more reliable? Is there a hook for after_publish_post or something similar?

Any thoughts appreciated.

1 Answer
1

No, no hook for this requirement. But the hook save_postworks on all changes and you can check for the current status and do it only, the status is in the right value as your requirement.

Leave a Comment