Getting WordPress Database Name / Username / Password Using PHP

I am writing a WordPress plugin and have to read the database name, username and password (in order to dump sql). Is it possible?

Thank -

+5
source share
2 answers

Yes, they are defined in wp-config.php

  • Database Name: DB_NAME
  • Database User: DB_USER
  • Database Password: DB_PASSWORD
  • Database Host: DB_HOST

They are determined. See wp-config.php in the Wordpress root directory

+13
source

Wordpress , OO-, , , Moxune. . WP_User:: __ set , .

, , , , - , aka wpdb::prefix - public, , dbname, dbpassword dbhost, protected public.

, Wordpress , . , .

class SaneDb
{
    private $_oDb;

    public function __construct(wpdb $oDb)
    {
        $this->_oDb = $oDb;
    }

    public function __get($sField)
    {
        if($sField != '_oDb')
            return $this->_oDb->$sField;
    }

    public function __set($sField, $mValue)
    {
        if($sField != '_oDb')
            $this->_oDb->$sField = $mValue;
    }

    public function __call($sMethod, array $aArgs)
    {
        return call_user_func_array(array($this->_oDb, $sMethod), $aArgs);
    }

    public function getDbName() { return $this->_oDb->dbname;     }
    public function getDbPass() { return $this->_oDb->dbpassword; }
    public function getDbHost() { return $this->_oDb->dbhost;     }
}

(functions.php) a global wpdb.

global $sanedb;
$sanedb = new SaneDb($wpdb);

$sanedb $wpdb.

, .

$sanedb->getDbName();
+3

All Articles