Wednesday, January 16, 2008

WordPress MU Optimization Script

WordPress MU will be automatically established more than 10 tables when a new user register, so when a large number of users register will caused a lot of problem. Then I write some performance scripts can delete the WordPress MU inactive users.

The PHP source code as follows:


  $mysql_host = "localhost";
  $mysql_user = "root";
  $mysql_pass = "";
  $mysql_db = "wordpress";
  $mysql_mylink = mysql_connect($mysql_host, $mysql_user, $mysql_pass)
  or die ("Cannot make the connection");
  mysql_select_db($mysql_db, $mysql_mylink)
  or die ("Cannot connect to the database");
  $strsql="select * from wp_blogs where blog_id>2 ";
  $rs_query = mysql_query($strsql);
  $alls=mysql_num_rows($rs_query);
  while (($RS = mysql_fetch_array($rs_query))) {
  extract($RS);
  $blogid=$RS["blog_id"];
  $query2 = "select * from wp_".$blogid."_posts ";
  $result = mysql_query($query2);
  $num= mysql_num_rows($result);
  //print $num;
  if ($num<=2) {
  print "DROP TABLE IF EXISTS wp_".$blogid."_comments;";
  print "DROP TABLE IF EXISTS wp_".$blogid."_links ;";
  print "DROP TABLE IF EXISTS wp_".$blogid."_options ;";
  print "DROP TABLE IF EXISTS wp_".$blogid."_postmeta ;";
  print "DROP TABLE IF EXISTS wp_".$blogid."_posts ;";
  print "DROP TABLE IF EXISTS wp_".$blogid."_terms ;";
  print "DROP TABLE IF EXISTS wp_".$blogid."_term_relationships ;";
  print "DROP TABLE IF EXISTS wp_".$blogid."_term_taxonomy ;";
  print "DELETE from wp_blogs where blog_id=".$blogid.";";
  }
  }
  mysql_close ();