Reputation: 805
I'm trying to save data to a file when a scheduled post is published. I tried to do this using the hooks 'future_to_publish', 'publish_future_post', 'transition_post_status' - none of them worked for me. I experimented with changing the priority of the hooks - didn't work either.
This is my code:
<?php
function savePostData( $ID ) {
$wpRootPath = get_home_path();
$postsDataPath = $wpRootPath . 'test.txt';
$handle = fopen( $postsDataPath, "w" );
fwrite( $handle, $ID );
fclose( $handle );
}
add_action( 'future_to_publish', 'savePostData', 10, 1 );
?>
When attaching the same function to the following hooks everything works:
add_action( 'publish_post', 'savePostData', 10, 1 );
add_action( 'pending_to_publish', 'savePostData', 10, 1 );
add_action( 'draft_to_publish', 'savePostData' );
I read there is some problem with the is_admin() function interfering with the wordpress cron job, but, still, I cannot get this to work.
Upvotes: 3
Views: 1865
Reputation: 805
It turned out when a post is published with cron, 'publish_post' hook is also executed and I didn't need to use it together with the 'future_to_publish' hook.
The problem with both hooks, however, was that for some reason get_home_path(); does not work in the same way as when the post is published immediately from admin panel, so using the exact path to my file solved the problem.
Upvotes: 2