WordPress file_put_contents on save_post

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.

Read More
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.

Related posts

Leave a Reply

2 comments

  1. pass in $post_id to your function

    function add_info($post_id) {
       $date = date('Y-m-d H:i:s');
    
       $file = "latest.json";
       file_put_contents($file, $date);
    
       update_post_meta( $post_id, 'latest', $date );
    }