Connecting your plugin to the WordPress Database

Connecting to the WordPress database is very simple. The $wpdb variable connects to the class which controls the access to the database meaning your plugin can easily use the database without having to write a connection script. The $wpdb class also contains routines which will make your life easier. This post details the $wpdb variable and the basic routines it supports. It is intended to help those building a plugin which needs a database and does not describe how to build a plugin.

Audience: WordPress Plugin Developers
Level: Beginner – Intermediate
Assumed Knowledge: You know how to build a WordPress Plugin and how to write queries in MySQL.

In this tutorial we are going to build a simple plugin which when a user visits a page in your WordPress environment their IP, the page visited, and time is stored in the database which the results will be shown in the admin section of WordPress. This plugin is going to be very basic yet you can easily imagine just how much analytics you could possibly do with the data captured.
Continue reading