Theme Development

Access Database Outside Of WordPress

Sometimes I've needed to build a new page outside of WordPress but still have the ability to access the WordPress database from this new page. This would normally use custom tables you've created.

To access the WordPress database normally you would just need to use the global variable $wpdb. This object connects to your database using the credentials found in the wp-config.php in the constant variables DB_NAME, DB_HOST, DB_USER, DB_PASSWORD.

function access_db()
{
   global $wpdb;
}

The $wpdb object has a number of methods that you can use to interact with the database, you are able to use the following:

  • query() - To run a custom SQL query on the database.
  • get_var() - Get a single value from the database and add as a variable.
  • get_row() - Get the single row from the database.
  • get_col() - Get the column information from the database.
  • get_results() - Get all the results from SQL query.
  • insert() - Insert a row into the database
  • replace() - Replace the data in the row.
  • update() - Allows you to update a row in the database.
  • delete() - Allows you to delete a row from the database.
  • prepare() - Used for SQL escaping when processing the SQL query.

If you're using custom database tables in your application then you will be able to access them by using the $wpdb object and writing your own custom queries.

For more information on creating custom tables in WordPress use this tutorial.

Custom Tables In WordPress

If you're creating a page outside of the WordPress application you won't have access to the $wpdb object, so you will need to instantiate the WordPress application before you can access it. To do this in WordPress it's very easy. If you look at the .htaccess file you will see some code like this.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

This will send all requests on the domain to go through the index.php file, then if you look inside the index.php file you will see how WordPress will be loaded, simply by loading the file wp-blog-header.php.

require( dirname( __FILE__ ) . '/wp-blog-header.php' );

When this is loaded it will create the WordPress application and instantiate all the variables that you will need. You will have access to the $wpdb object which will automatically connect to the datbase in the wp-config.php file.

Now you will be able to use this variable in your new page outside of the WordPress application.

Create a new file, this example is a new file at the root level of your WordPress files. First require the wp-blog-header.php file and this will instantiate WordPress outside of the application.

<?php
// Get access to WordPress
require( dirname( __FILE__ ) . '/wp-blog-header.php' );

// Get all posts
$posts = $wpdb->get_results('SELECT * FROM '. $wpdb->prefix.'posts');
Back to top

Learn how to code with Treehouse

  • Learn projects with access to 1000+ videos
  • Practice live with our Code Challenge Engine
  • Get help in our members-only forums

Start with a 7 day free trial