Anshad Vattapoyil
Anshad Vattapoyil

Reputation: 23463

How to call wpdb in a WordPress plugin class

How can I call the wpdb inside a plugin function defined in a class like this,

<?php

/*
*   Plugin Name: Test
*   Plugin URI: http://www.test.com/
*   Description: Test plugin for wordpress.
*   Version: 1.0.0
*   Author: Test
*   Author URI: http://test.me/
*/      

class Test {

    /*
    |------------------------------------
    | Constructor
    |------------------------------------
    */

    public function __construct() {

    }

    public function abc() {
        var_dump($wpdb);
    }
}

Upvotes: 0

Views: 3335

Answers (1)

JayDeep Nimavat
JayDeep Nimavat

Reputation: 476

It is a global variable, $wpdb, which is an instantiation of the class already set up to communication WordPress database. Always use the global $wpdb variable. (Remember to globalize $wpdb before using it in any custom functions.)

You need to use it like this.

global $wpdb;
//do something with it.

Upvotes: 3

Related Questions