开发者

Editing a post after it's been published via a plugin?

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.";

    $p开发者_开发百科ost_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.

function edit( $post_ID ) {
    static $plugin_has_updated = false;
    if ($plugin_has_updated) return;
    $plugin_has_updated = true;
    $content = "Hello. This is a test.";
    $post_arr = array("ID"=>$post_ID, "post_content"=>$content);
    wp_update_post( $post_arr );
}

add_action('publish_post', 'edit');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜