最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

database - How to initialize $wpdb?

programmeradmin0浏览0评论

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

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 asked Jun 18, 2012 at 13:58 DailDail 172 gold badges2 silver badges7 bronze badges 2
  • Any reason why you need to have .php page in your theme and not do it through WordPress? – Stephen Harris Commented Jun 18, 2012 at 14:10
  • @StephenHarris, I needed it to make some mass database seeding. – Akshay K Nair Commented Jan 21, 2022 at 8:49
Add a comment  | 

5 Answers 5

Reset to default 3

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.

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.

First, notice what user HungryCoder is saying is correct: your file, is not being liked to WordPress

One possible solution, that will work on your dev, and Linux servers is:

include( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');

Excellent solution is:

my_wpdb('test','pass',  'database', 'host', '../path/to/wp/');

function

function my_wpdb($user, $pass, $db, $host, $path_to_root= '/../../../../../../', $run_wp_config=true){
    $path_to_wp=  dirname(__DIR__) .$path_to_root;
  //execute wp-config, if not run already
    if($exec_config && !defined('DB_HOST')){    preg_match('/\<\?php(.*?)\/\* That\'s all, stop editing/si',  file_get_contents($path_to_root."wp-config.php"), $found);    eval($found[1]);    }
    require_once( $path_to_wp.'wp-includes/wp-db.php' );    if ( file_exists($path_to_wp.'wp-content/db.php' ) ) require_once($path_to_wp.'wp-content/db.php' );
    return $GLOBALS['wpdb'] = new wpdb( $user, $pass, $db, $host );
}

Since immediately after solving this issue begins a new one - where to PUT the initialization (it's simple, but still if you don't know this, you may do some nasty things instead - like trying to modify wp-db.php). The answer is kind of implicitly hidden in @sorich87's answer, so just let's add it here explicitly:

Create db.php script in your 'wp-content/' folder.

Also, for more clean initialization than just throwing around globals, see https://stackoverflow/a/5418972

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论