Using the WPDB Class

The wpdb class can be used to execute SQL query statements which return PHP objects
In order to work with the class you have to declare it as a global variable.

global $wpdb;

For OOP I’ve found it best to declare the wpdb object in a custom database class constructor and assign the class to a class property for ease of reuse.

public $wpdb;

public function __construct()
    global $wpdb;
    $this->wpdb = $wpdb;

Table Names

It is possible to assign prefixes to WordPress database table names. This is done when the WP app is installed. In order to properly work with the WPDB, make sure to append the prefix property from the wpdb object to all table names.

$table_name = $wpdb->prefix . 'posts';
$sql_query = "SELECT * FROM $table_name LIMIT 10";

WP also has built-in table names for working with default tables: $wpdb->posts, $wpdb->postmeta, and $wpdb->users.

Query Helper Methods

WP has built-in helper methods for working with basic database queries: $wpdb->insert(), $wpdb->update(), and $wpdb->get_row().

Using an External Database

To connect to an external database, create a new instance of the wpdb class.

// Connect to an external database.
$extdb = new \wpdb($username, $password, $db_name, $db_host);

// test the new DB object.
if (is_object($extdb) && !isset($extdb->error)) {
    // DB object is good.

Theme Development

Themes are a set of files containing PHP and HTML which sit on-top of the core WordPress application and convert it’s raw response data in to usable web pages. Themes determine the look and functionality of your WordPress website.

Child Themes

A child theme allows you to change or add things to an existing theme, without having to create the theme and all it’s functionality from scratch.

A child theme defines only the differences between it and it’s parent theme. WordPress first looks at the child for files but falls-back to the parent if nothing is found.

File Structure

  • Every WordPress theme (child or parent) needs a directory in the /wp-content/themes directory.
  • Although not required, a child theme directory should be named after the parent theme’s directory by adding -child. For example, if you are creating a child theme for the twentyseventeen theme, the child directory would be named twentyseventeen-child.
  • Each theme directory requires at least the two files style.css and functions.php. There are no limits to what additional files and directories can be placed in a theme directory.
  • If you want to use object-oriented coding methods in your theme you can create an optional directory to hold your class files.
       /functions.php  // Required file.
       /style.css      // Required file.
       /Libs           // Optional directory for OOP class files.


The style.css file for a theme starts with a comment doc-block which defines information about the theme.

Only the Theme Name and Template are required for WP to detect and use a theme, however a standard theme usually comes with a Description, Author, and Version as well. If you plan to publish your theme you will need to add more info.

 * Theme Name:   My Theme
 * Description:  A child theme of the Twenty Seventeen default WordPress theme
 * Author:       Daniel Watts
 * Template:     twentyseventeen
 * Version:      1.0.0


The functions.php file allows you to add and change functionality and features to your website.


// When WP calls the `wp_enqueue_scripts` action hook, the `enqueue_styles` function is called.
add_action( 'wp_enqueue_scripts', 'enqueue_styles' );

// This function injects the `style.css` file from the parent theme into the doc head.
function enqueue_styles() {
   wp_enqueue_style( 'parent-theme-styles', get_template_directory_uri() . '/style.css' );

Object Oriented functions.php


$dw_theme = new Theme_Functions();



class Theme_Functions
    public function __construct()
        add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) );

    public function enqueue_styles()
        wp_enqueue_style( 'parent-theme-styles', get_template_directory_uri() . '/style.css' );

More Resources