There are a number of hooks you can use, such as publish_post
or save_post
, e.g.:
// an example of a post save hook
add_action( 'save_post', 'diligents_post_save_hook' );
function diligents_post_save_hook( $post_id ) {
//verify post is not a revision
if ( !wp_is_post_revision( $post_id ) ) {
// do things
}
}
save_post
will fire on publish and update, so be aware it will fire on saving of drafts etc. publish_post
will run on publish, keep this in mind as it will not fire on update.
Saving data to a file is a step I cannot cover, as it is not a WordPress question, but a standard PHP question. For details on writing, reading, and saving to files, reference PHP.net