[wp-hackers] post revisiniong API

Mark Cunningham mark.cunningham at gmail.com
Thu Feb 26 10:19:53 GMT 2009


2009/2/24 Joost de Valk <joost at yoast.com>:
> Hey,
>
> Has anybody ever tried to create a post revision from within a plugin? I
> like the idea of the SEO Smart Links plugin, but don't want it to do the
> filtering on each page load, I'd way rather have it go through each post
> once, create an "original" revision and a new revision with its own
> output...

Yes I have created and managed revisions from within one of my
plugins. My plugin creates posts and it also edits posts, so I wanted
to make sure that it didn't create multiple unnecessary revisions and
also use revisions, if avaliable, in a useful way.

I don't know how useful it'll be but this what I did (so far - it's a
work in progress). So on the create post code, at the end I put this
(pretty much ripped from the internals of the revision API anyway):

// Delete all versions but actual
if( WP_POST_REVISIONS ) {
   $revisions = wp_get_post_revisions( $post_ID, array( 'order' => 'ASC' ) );
   foreach($revisions as $rev) {
      wp_delete_post_revision( $rev->ID );
   }
}

Then on the edit post, I wanted the edits and updates to go on the
custom revision and if they are good, the user can promote them to the
real post.

// if versioning enabled, use a revision
$revision_id = wp_save_post_revision($post_id);
if($revision_id == NULL) {
   // revision is good, now just used $revision_id instead of $post_id
to do any update (including add_post_meta)
}

If the edits are good, then do this:

_wp_put_post_revision($revision_id);

If you want to drop the edits for ever:

wp_delete_revision($revision_id);

I haven't fully tested it as this feature is a work in progress, but
so far my initial tests show that it works. The only annoying thing is
that the Custom Fields you add on the revision id, don't show up if
you do a diff in the Wordpress UI but do seem to get copy over if you
do a _wp_put_post_revision. I can't seem to find a hook/filter to
allow me to add Custom Fields to the diff.

Hope you find it useful,

Mark
-- 
http://wordpress.org/extend/plugins/tdo-mini-forms/
http://thedeadone.net
http://lostheroesrpg.com


More information about the wp-hackers mailing list