I need to write to a file when I save a post in WP. This is just a basic example– I’ll be writing a bunch of other content to this file, not just the date. That being said, this basic example is not working. Setting an absolute path did not help either.
The update_post_meta is just there to verify that the function did indeed run.
function add_info() {
$date = date('Y-m-d H:i:s');
$file = "latest.json";
file_put_contents($file, $date);
update_post_meta( $post_id, 'latest', $date );
}
add_action( 'save_post', 'add_info', 10, 3 );
Also, neither ‘save_post’ nor ‘post_updated’ work, but ‘after_setup_theme’ does.
It turns out I needed absolute path back to the server root:
or
pass in $post_id to your function