WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm writing a plugin. Can anyone please show me how to edit 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";

add_action('publish_post', 'edit');

Although I'm having no luck. Can anyone suggest how to get it working? Thanks for the help.

share|improve this question
I think it would help if you explained why you doing this, ie. what's the end goal here? Are you trying to pull a particular post, modify a property then save that change? More details about what you're trying to accomplish would be most helpful for anyone reading.. – t31os Aug 1 '11 at 14:18
Ah, sorry. I've made a plugin which gets the contents of a post and then replaces various words, etc. and then sends it to an email address. That's all fine and works great. It's just I want the changes to be saved to the actual post as well. :) – Joey Morani Aug 1 '11 at 14:28
Ok, but at what point do you need to make this change, when you load a particular plugin page, when you click link on a given page, or when a post is saved? When should this occur and is intended as an automated action, or one you perform by clicking something? Based on what you've said i believe Scribu has already given you the info you need(which is why i think there may be more to it than your question is detailing). – t31os Aug 1 '11 at 14:33
I want the change to be done after it's been published/created. I've been using "publish_post" which changes it after the post has been published. That works and it changes the content, but it just enters a loop because changing the content causes it to be published again. – Joey Morani Aug 1 '11 at 14:44
Try the wp_insert_post_data hook. – t31os Aug 1 '11 at 14:47

You modified a copy of a row from the wp_posts table.

You need to send the modified version back to the database:

wp_update_post( $post_info );

share|improve this answer
Thanks, although it's making the plugin time out. It seems to edit the database though, but just times out while doing it. – Joey Morani Aug 1 '11 at 10:09
Ah, right. It's because it enters an endless loop. Try replacing the 'publish_post' hook with something else, like 'transition_post_status'. – scribu Aug 1 '11 at 10:17
Nope. No luck. Does the same thing although it doesn't actually edit the post when using 'transition_post_status'. Anything else I can try? :) Thanks again. – Joey Morani Aug 1 '11 at 10:59
Woops. I've got a "Internal Server Error" when going to post-new.php now. :L – Joey Morani Aug 1 '11 at 11:02
Are you sure you need to store the changes in the database? Wouldn't it be better if you did it before it's displayed? You can do this via 'the_content' filter. – scribu Aug 1 '11 at 11:09

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.