How to include $wpdb in wordpress plugin?

30,939

Solution 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.

Solution 2

get away from hard solutions! and just use :

define( 'SHORTINIT', true );
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );
Share:
30,939
Michiel Standaert
Author by

Michiel Standaert

Hello community, I am a young developer that has just started his own company, but also wants to play an active role as an opensource contributor. If you have any questions, feel free to contact me through my blog (http://michielstandaert.be/blog).

Updated on July 09, 2022

Comments

  • Michiel Standaert
    Michiel Standaert almost 2 years

    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');
    }
    
  • Michiel Standaert
    Michiel Standaert almost 13 years
    I do have to include them manually, because if i want to trigger an excel download, i cannot do that from within the admin.php of wordpress (the header information cannot be passed, don't ask me why). So i call a php-file from my plugin-directory, but this cannot access the $wpdb variable or any other variable i would need to access the wordpress db for that matter.
  • Lukas Knuth
    Lukas Knuth almost 13 years
    The header information can't be send because you change the header after something was already printed. You should (like i said) create a new class in a new file and pass it the $wpdb-Object.
  • Michiel Standaert
    Michiel Standaert almost 13 years
    And how should i pass the $wpdb-variable to that class? because the way i am calling the file now is by redirecting to another php-page. (html shown below). <a href="../wp-content/plugins/myplugin/to_excel.php?filter=<?p‌​hp echo($filter_name) ?>" class="button-primary">Save as excel</a>
  • Lukas Knuth
    Lukas Knuth almost 13 years
    Sorry, that was a misunderstanding on my side. I updated my post, take a look at it.
  • Michiel Standaert
    Michiel Standaert almost 13 years
    Thank you for replying, I have gotten a little closer to solving the problem. I have updated my original post with some more explanation.