Blog Home > Website Development

How to Use Wordpress Functions Outside the Theme Folder

Posted by: Matt Dunlap on Wednesday, September 30th, 2009

Wordpress has some really good functions and classes that help developing a website quickly. Sometimes you want to make a webpage that is outside your wordpress theme but still use classes like the database class ($wpdb). It is very simple. All you have to do is inlcude the wp-config.php file located in your website root folder into any webpage that you want to access wordpress functions.

I’ve had to use thisĀ  in a few different scenarios.

  1. Writing a plugin where you have some files that are not activated when you activate the plugin. A very simple plugin will have a single file that contains the functions needed to activate the plugin, add the menu items, and do the job of the plugin. What if you are using AJAX and you need to access some data from the database but the file is not the main plugin file. You will need to include wp-config.php for this to work.
  2. If you make a file in the root folder and include wp-config.php you can then access get_header(), get_footer(), etc… even though you are outside the theme folder. The header for this file and webpage will be the same header as the current selected theme.

This can be used for a webpage located in the root folder.

include('wp-config.php');
global $wpdb, $table_prefix;
//this will now allow you to access the database and if using wpmu, use the table_prefix to access the proper website data

Leave a Reply








Don't be shy, Speak your mind

If you have any questions, concerns or general comments about this post, please leave a comment.
Show us your face by signing up for a FREE Gravatar at Gravatar.com! Don't be a faceless voice in the crowd.
Trackback URL: