I’m trying to write a plugin which edits the content of a published post. I’ve tried using this:
function edit( $post_ID ) {
$content = "Hello. This is a test.";
$post_info = get_post($post_ID);
$post_info->post_content = "$content";
wp_update_post( $post_info );
}
add_action('publish_post', 'edit');
Although that’s not working. It enters a loop (because it’s being published again) and only ends when it times out. Would there be another way to do this?
I think you have to have a static variable in the function that tracks whether the function has been called. Also, wp_update_post takes an array rather than an object — at least that’s the way I do it.