Always use the wpdb WordPress Database Class

March 2012 ยท 1 minute read

I just had a troubleshooting conference call with a developer and we ran into an issue with a site I installed into a WordPress Multisite instance. At first glance it looked like it was a low-level PHP error since a foreach function was throwing an Invalid Argument error.

Digging deeper the developer found that the code that he wrote was using a hard-coded table reference to wp_options. I told him we are using a multisite instance and the fix was clear. There is no wp_options table, but there is a wp_8_options table specific to the site.

He replace that reference with…

' . $wpdb->prefix . 'options

…and viola. We both learned a lesson from that.

Always use the wpdb class to interact with the database so your code is as modular as possible.