public
Last active

WordPress SE 71248: Delete revisions on post publish

  • Download Gist
wpse71248-del-revisions.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
<?php
/**
* Plugin Name: WPSE71248 Delete Revions on Publish Posts
* Plugin URI: http://wordpress.stackexchange.com/questions/71248/
* Description:
* Version: 1.0.0
* Author: Frank Bültge
* Author URI: http://bueltge.de
* License: GPLv3
*/
 
! defined( 'ABSPATH' ) and exit;
 
add_action( 'publish_post', 'fb_remove_revisions' );
 
function fb_remove_revisions( $post_id = FALSE ) {
$post_id = (int) $post_id;
$revisions = '';
// Get the revisions
$revisions = new WP_Query( array(
'post_status' => 'inherit',
'post_type' => 'revision',
'showposts' => -1,
'posts_per_page' => -1,
'post_parent' => $post_id
) );
if ( empty( $revisions ) )
return $post_id;
 
// Remove the revisions the non-core-way
global $wpdb;
foreach ( $revisions->posts as $revision ) {
$query = $wpdb->prepare(
"
DELETE FROM $wpdb->posts
WHERE ID = %d
AND post_parent = %d
",
$revision->ID,
$post_id
);
$wpdb->query( $query );
}
return $post_id;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.