Monday, June 4, 2012

wordpress mysqli class

So recently as it usually a client of ours come with a request for fresher looking website and blog based on wordpress. No big deal, we said we do those with our eyes closed. So after all the development has been finished and we were ready to push it to production server we hit a brick wall. Production server is running a legacy application on php 5 and mysql 4.1 with just mysqli module. So no big deal still - yum update blah blah and all set but not so fast. Server was running a legacy Fedora v1. Getting a push back from client on migration to cloud we had to improvise with mysqli and wordpress. So with two developers exploring options and playing around we finally hit the jackpot with mysqli wpdb class from wordpress's own repo at http://plugins.svn.wordpress.org/performance-testing/trunk/library/db.php and few additions (functions like db_version, tables, get_blog_prefix and class variable $ms_global_tables) to make it work with our version of wordpress (3.1+).

Keep in mind that you should place this file in wp-content folder under name db.php. If you get any errors about wpdb class cannot be re-declared than you know that original wp-includes/wp-db.php file is being loaded before your custom wp-content/db.php so you have two options:
  1. Overwrite contents of wp-includes/wp-db.php with that of wp-content/db.php
  2. Change wp-includes/load.php function require_wp_db() to load original wp-includes/wp-db.php ONLY if wp-content/db.php does not exist. End result:



Here is the final code: