Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a .php page in the theme root to check the data of one CUSTOM form. After receiving this data I need to do a query in a custom mysql table, so I need $wpdb, but I can't use it directly (or doing global $wpdb) because it is a phisical .php file, so the rewrite rule will not affect the request (the request is not passed to index.php because the file exists).

So In this case how can I create an object $wpdb?

Thanks

share|improve this question
    
Any reason why you need to have .php page in your theme and not do it through WordPress? –  Stephen Harris Jun 18 '12 at 14:10

2 Answers 2

up vote 1 down vote accepted

First of all you probably created an isolated file that is not under the umbrella of WP files. That's why you are not getting $wpdb. I guess you may not following the general rules/conventions of theme development. my question is now how are you accessing the file?

whatever, if you include the wp_config.php in your file, you will get the $wpdb in your file.

considering in a directory under themes directory, here is how you include/require the file

require_once ('../../../wp-config.php');

you may need to alter the path based on your system.

share|improve this answer
require_once( 'path/to/wordpress/wp-includes/wp-db.php' );
if ( file_exists( 'path/to/wordpress/wp-content/db.php' ) )
    require_once( 'path/to/wordpress/wp-content/db.php' );

$wpdb = new wpdb( 'user', 'password', 'database', 'host' );

To see how WordPress initialize it, see wp-includes/load.php, line 326.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.