How to Use WordPress Functions Outside the Theme Folder

By Matt Dunlap on September 30th, 2009 8 comments
Tags: , ,

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

My next post is going to be awesome... Subscribe to my feed so you don't miss it

Large RSS Feed Button


Previous Post Next Post

8 Responses to How to Use WordPress Functions Outside the Theme Folder

james
Comment added: 14 March, 2010 at 6:46 am

cheers mate EXACTLY what i wanted to find (Y)

Matt Dunlap
Comment added: 14 March, 2010 at 11:04 am

Glad to help

Kayleigh
Comment added: 19 March, 2010 at 6:52 am

Thanks, I’ve been looking at countless sites for this :)

Ben
Comment added: 29 April, 2010 at 2:48 am

Thanks man, this helped me saving a huge amount of time!

Paul
Comment added: 19 May, 2010 at 8:22 am

I need to include wordpress header and footer (and all related wp functions)in my WHMCS installation (uses smarty templates) Will your methods work?

Will pay to have this done. Please get back to me asap.

Thanks,

Andy Blake
Comment added: 18 June, 2010 at 12:50 pm

I'm a total newbie, and I'd like to include the Anything Slider on a page (which has no posts). Can you help? I'm missing something, but I can't figure out what. What does it mean to call this: <?
$number_of_posts = 10 //optional, default is 5
. anything_content_slider($number_of_posts);
?>
Thanks!

Matt Dunlap
Comment added: 18 June, 2010 at 9:39 pm

Andy, that is PHP. It's server side code WordPress is written in. If you want to use the anything slider on a page, you should search how to make a page template in wordpress and then add that PHP code you referenced.

Kate Mag
Comment added: 20 August, 2010 at 12:12 am

great tip. Thanks.

Post a Comment

Name:   
Email:   
Website:   
Comments:   
alert icon Do you need help with your website? I can offer full system admin, website development and Internet marketing services
Domain Name Deals
.COM for $1.99
With any non domain name purchase
Free Private Registration - When you buy or transfer 5 domains at opendls.com
Domain Name Reseller Accounts
Low buy rates - Very Profitable
Matt Dunlap on Why am I getting redirect loop errors with FireFox 3.6.8?
Hi, thanks for the heads up... Have you experienced this problem with any other websites? It's reall[...]
Stacy on Why am I getting redirect loop errors with FireFox 3.6.8?
I was attempting to follow your links to these tutorials from your youtube video, and I was using th[...]
Matt Dunlap on 301 redirects will lower your Google Pagerank
You want 301 redirect on same site redirects mainly to eliminate duplicate content. For example, if [...]
Kate Mag on 301 redirects will lower your Google Pagerank
great info. Does this mean that only affecting 301 redirect to another domain. I have 301 redirect t[...]
Mike Johnson - Real Estate Agent on Why is making extra money a bad habit?
Hey man, this is some great stuff. The only thing I can't figure out is why this sort of thing isn'[...]

Top Guest Bloggers

LoneWolf
View Posts | RSS

Guest Bloggers Welcome!


Powered By: Free Premium WordPress Theme | Copyright 2007 - 2010 mattdunlap.org Sitemap