How to include $wpdb in wordpress plugin?

2019-02-07 08:28发布

问题:

I've been developing for some time on a plugin in wordpress, but one problem keeps bugging me. I want to export a database-table as an excel file and therefor i need access to the global $wpdb->variable from a file in my plugin directory.

I found a blog entry that explains what classes i should include, but this doesn't work (link is below). As you can see, i do a var_dump, but it never reaches that point. If i leave the includes of wp-config and wp-load out of the code, the dump returns NULL, so i'm guessing there is a problem with the imports.

Anyway, i was hoping someone could help me with this problem. I don't necessarily need a fix for my approach, I just need a way to export an array of data (fetched from my db) to excel in wordpress. Any help would be appreciated. Thanks in advance.

http://www.notesbit.com/index.php/web-mysql/web-scripts/standalone-access-the-wordpress-database-using-wpdb/

include_once('../../../wp-config.php');
include_once('../../../wp-load.php');
include_once('../../../wp-includes/wp-db.php');

var_dump($wpdb);
$filter = get_where_clause();
$order = get_order_by_clause();

$data = $wpdb->get_results("SELECT * FROM " . $table_prefix . "team_data" . $filter . $order, ARRAY_A);

$result = array();

EDIT: I cannot include the wp-config, it gives constant errors. I know where the bug is taking place, I just need to find a work-around. when looking at the wp-settings page (which is included by the wp-config) you will find this line of code:

foreach ( wp_get_active_and_valid_plugins() as $plugin )
    include_once( $plugin );
unset( $plugin );

this is where there is a bug. I just don't know how i should work around this bug.

EDIT 2: Problem solved. When including the file, i included the wp-config more than once (even though i stated it should only be included one time). I solved the problem by using the following code.

global $wpdb, $table_prefix;

if(!isset($wpdb))
{
    require_once('../../../../wp-config.php');
    require_once('../../../../wp-includes/wp-db.php');
}

回答1:

If you're creating a WordPress plugin, you don't need to include those files manually.

If you want to export your table, why don't you create a function/class for it and pass the $wpdb to it (if you need it). You can also use the normal MySQLi-class (from PHP) do access your MySQL Database.


If you simply want to access the MySQL Database with the stored login-values which are used by WordPress, you can include the wp_config-file from the WordPress root-directory. It has some (self explaining) global fields which you could use to connect to your database:

include "WP-ROOT-PATH/wp-config.php";
$db = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Test the connection:
if (mysqli_connect_errno()){
    // Connection Error
    exit("Couldn't connect to the database: ".mysqli_connect_error());
}

After that you have a instance of the MySQLi-class (as mentioned above) which you can use to access your Database.

I'm however not sure if this is the perfect way to go but it sure works.


To Debug WordPress (if something doesn't work and there is no Error-Message) you should activate debugging in the wp-config.php-file:

/**
 * For developers: WordPress debugging mode.
 *
 * Change this to true to enable the display of notices during development.
 * It is strongly recommended that plugin and theme developers use WP_DEBUG
 * in their development environments.
 */
define('WP_DEBUG', true);

Also, if you're testing your PHP-Scripts on a local server, you should turn the display_error to on in your php.ini-file:

; This directive controls whether or not and where PHP will output errors,
; notices and warnings too. Error output is very useful during development, but
; it could be very dangerous in production environments.
display_errors = On

However this should only be done on your local test-server, not in a productive scenario.