Title: Using $wpdb outside wordpress
Last modified: August 19, 2016

---

# Using $wpdb outside wordpress

 *  Resolved [dcole07](https://wordpress.org/support/users/dcole07/)
 * (@dcole07)
 * [17 years, 9 months ago](https://wordpress.org/support/topic/using-wpdb-outside-wordpress/)
 * I’m writing a plugin, but a AJAX request is calling a php file and I want to 
   create tables and save data that have the same prefix as the other WordPress 
   tables.
 * The problem is you can NOT just global $wpdb and have it work, because no other
   files are involved! What file should I include so that the $wpdb could be global?

Viewing 15 replies - 1 through 15 (of 18 total)

1 [2](https://wordpress.org/support/topic/using-wpdb-outside-wordpress/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/using-wpdb-outside-wordpress/page/2/?output_format=md)

 *  [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [17 years, 9 months ago](https://wordpress.org/support/topic/using-wpdb-outside-wordpress/#post-793323)
 * Probably `wp-includes/wp-db.php`.
 *  [whooami](https://wordpress.org/support/users/whooami/)
 * (@whooami)
 * [17 years, 9 months ago](https://wordpress.org/support/topic/using-wpdb-outside-wordpress/#post-793347)
 * whenever you want to use wordpress fucntions OR variables outside of wordpress,
   you need to include `wp-blog-header.php`
 *  [WordyPresser](https://wordpress.org/support/users/jeremyduffy/)
 * (@jeremyduffy)
 * [17 years, 8 months ago](https://wordpress.org/support/topic/using-wpdb-outside-wordpress/#post-793701)
 * How do you include it? I know it’s in the root, but how do I reference it? I 
   tried many things, but it either didn’t work or gave me an access denied error.
 * All I want is to be able to reference the WPDB value in my AJAX handler file.
 *  [whooami](https://wordpress.org/support/users/whooami/)
 * (@whooami)
 * [17 years, 8 months ago](https://wordpress.org/support/topic/using-wpdb-outside-wordpress/#post-793702)
 * > How do you include it?
 * [http://us.php.net/include/](http://us.php.net/include/)
 * there are hundreds, if not thousands, of examples of php includes inside all 
   of the wordpress files themselves.
 * Actually, thinking about that again the devs tend to use requires but the format
   is identical. 🙂
 *  [WordyPresser](https://wordpress.org/support/users/jeremyduffy/)
 * (@jeremyduffy)
 * [17 years, 8 months ago](https://wordpress.org/support/topic/using-wpdb-outside-wordpress/#post-793703)
 * To be specific, I have a template that calls an AJAX function and loads another
   file (which isn’t a template, just a PHP file). How can I have WordPress functions
   and variables available in that file?
 *  [WordyPresser](https://wordpress.org/support/users/jeremyduffy/)
 * (@jeremyduffy)
 * [17 years, 8 months ago](https://wordpress.org/support/topic/using-wpdb-outside-wordpress/#post-793704)
 * Whooami, I used include, but it gave me an error. Also, how do I reference it?
   I can just use “/wp-blog-header.php” for the one in root, but what about when
   the wordpress install isn’t in the root? How do I find it or does it just have
   to be hard-coded?
 *  [whooami](https://wordpress.org/support/users/whooami/)
 * (@whooami)
 * [17 years, 8 months ago](https://wordpress.org/support/topic/using-wpdb-outside-wordpress/#post-793705)
 * did you look at the link I provided?
 * You reference it, using the **path to it**, **from the file you are including
   it in**.
 * do we need a primer on paths?
 * ./ == current directory
    ../ == one directory up ../stuff == one directory up**
   and** then into stuff.
 * ../../ == 2 directories up.
 * ../../morestuff == 2 directories up **and** then into morestuff
 *  [WordyPresser](https://wordpress.org/support/users/jeremyduffy/)
 * (@jeremyduffy)
 * [17 years, 8 months ago](https://wordpress.org/support/topic/using-wpdb-outside-wordpress/#post-793706)
 * Error:
    `Warning: require(./wp-blog-header.php) [function.require]: failed to
   open stream: No such file or directory`
 *  [whooami](https://wordpress.org/support/users/whooami/)
 * (@whooami)
 * [17 years, 8 months ago](https://wordpress.org/support/topic/using-wpdb-outside-wordpress/#post-793707)
 * ./things == inside the current directory and then into the subdirectory named
   things.
 * —
 * If you stop pasting, and typing and start reading ..
 *  [WordyPresser](https://wordpress.org/support/users/jeremyduffy/)
 * (@jeremyduffy)
 * [17 years, 8 months ago](https://wordpress.org/support/topic/using-wpdb-outside-wordpress/#post-793708)
 * Hmm… I suppose the themes are always the same depth… I just don’t want to make
   it difficult for people who use my code and have non-standard installations. 
   So I suppose I’ll have to use require(“../../../wp-blog-header.php);
 * (using that, it’s working. As long as no one is putting their theme and deeper
   or more shallow than normal, I guess it will work).
 * Thanks!
 *  [whooami](https://wordpress.org/support/users/whooami/)
 * (@whooami)
 * [17 years, 8 months ago](https://wordpress.org/support/topic/using-wpdb-outside-wordpress/#post-793709)
 * if you are doing this for a plugin, there are other options. there are all kinds
   of preassigned variables that could handle simplifying that.
 *  [WordyPresser](https://wordpress.org/support/users/jeremyduffy/)
 * (@jeremyduffy)
 * [17 years, 8 months ago](https://wordpress.org/support/topic/using-wpdb-outside-wordpress/#post-793710)
 * Not a plugin in this case. Actual file here:
 * [http://www.jeremyduffy.com/advanced-blog-search/](http://www.jeremyduffy.com/advanced-blog-search/)
 *  [Glenn Ansley](https://wordpress.org/support/users/blepoxp/)
 * (@blepoxp)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/using-wpdb-outside-wordpress/#post-793726)
 * Does anyone know how I would go about doing this now that the wp-content directory
   can be moved if the blog isn’t in the site root?
 *  [Glenn Ansley](https://wordpress.org/support/users/blepoxp/)
 * (@blepoxp)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/using-wpdb-outside-wordpress/#post-793727)
 * Nevermind: I wound up just passing the ABSPATH constant through the ajax request
   as a var. Then I included the file I needed.
 * `mysack.setVar( "wp_root" , "<?php echo ABSPATH;?>" );`
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [17 years, 5 months ago](https://wordpress.org/support/topic/using-wpdb-outside-wordpress/#post-793728)
 * … Ugh.
 * If you have to have an external file include the WP functions because you’re 
   referencing them directly like that, then you’re pretty much doing it wrong.
 * Consider alternate approaches, like passing the actual data you need to the files
   directly, or generating the files on the fly from the plugin, or some such thing.
 * Because every time you include the entirety of WordPress again when only one 
   page is being viewed, you increase the server load by double. And God kills a
   kitten.

Viewing 15 replies - 1 through 15 (of 18 total)

1 [2](https://wordpress.org/support/topic/using-wpdb-outside-wordpress/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/using-wpdb-outside-wordpress/page/2/?output_format=md)

The topic ‘Using $wpdb outside wordpress’ is closed to new replies.

## Tags

 * [global](https://wordpress.org/support/topic-tag/global/)
 * [wpdb](https://wordpress.org/support/topic-tag/wpdb/)

 * 18 replies
 * 7 participants
 * Last reply from: [superbibi75](https://wordpress.org/support/users/superbibi75/)
 * Last activity: [17 years, 5 months ago](https://wordpress.org/support/topic/using-wpdb-outside-wordpress/page/2/#post-793731)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
