How to remove post revisions?

February 16th, 2009 Posted in Wordpress MU

We will show you today how to remove existing post revisions from all blogs. This is a very simple about 20-LOC-script (LOC = Lines Of Code) 😉

How should it work?

  • Create a menu entry
  • Check if the user has enough rights to run this script
  • Get a list of all blogs
  • Go to each post table and delete posts marked as “revision”

Create a menu entry

First you have to create a file named “mg_clean_revisions.php”. Open the file to put some code into it. At first we need a hook which will be called when you access the admin menu. This hook should run a function which creates a menu entry.

  1. // H O O K
  2. add_action('admin_menu', 'mg_clean_revisions_menu');
  3. // Create menu entry
  4. function mg_clean_revisions_menu() {
  5. add_submenu_page('wpmu-admin.php', 'Clean Revisions',
  6. 'Clean Revisions', 1, 'mg_clean_revisions.php', 'mg_clean_revisions');
  7. }

A menu entry “Clean Revisions” will be created. When you click on this menu, the script will call a function named “mg_clean_revisions”. Now we have to implement this function. When this function is called we should check if the user has enough access rights to do this. Then we need a list of all blogs that we can delete posts marked as “revision”.

  1. function mg_clean_revisions() {
  2. global $wpdb;
  3. echo "<h1>My-Guide's Clean Revisions</h1>";
  4. // Check Whether User Can Manage Database
  5. if(!current_user_can('manage_database')) {
  6. die('Access Denied');
  7. }
  8. // Get all blogs
  9. $blog_list = $wpdb->get_results( "SELECT blog_id FROM $wpdb->blogs");
  10. // Go trough all blogs and delete revisions
  11. foreach ($blog_list as $blog) {
  12. $table = $wpdb->base_prefix . $blog->blog_id . '_posts';
  13. $wpdb->query("DELETE FROM $table WHERE post_type = 'revision'");
  14. print "Cleaning: " . $table . " ..done<br>";
  15. }
  16. echo "<br><strong>All revisions deleted.</strong>";
  17. }
You can also download this wordpress mu plugin.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • MisterWong
  • Y!GG
  • Webnews
  • Digg
  • del.icio.us
  • StumbleUpon
  • Reddit

Comments are closed.

My-Guide Interessante Guides: LaTeXSerbienSparenNewsKündigung