How To Access WordPress Database Details in Plugin Tutorial

Share this Article
Reading Time: 4 minutes
1,643 Views

Inside this article we will see how to access wordpress database details in plugin development. WordPress details means username, password, database name, etc.

In plugin / theme development sometimes we need to fetch database details directly to use. WordPress works on in very structured way. We can easily access anything what we want to be used or customized.

Examples where we need to database details like –

  • When we execute shell commands to application.
  • Use MySQLdump commands via PHP functions.

For database details, wordpress have global constants available which we can use for accessing database connection string values.

Learn More –

Let’s get started.


Constants For Database Settings

There are several global constants available in wordpress to use inside wordpress development. Here, we will see about database connection global constants only.

Open wp-config.php from project root.

//...

// ** Database settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'wp_shopping' );

/** Database username */
define( 'DB_USER', 'admin' );

/** Database password */
define( 'DB_PASSWORD', 'root' );

/** Database hostname */
define( 'DB_HOST', 'localhost' );

/** Database charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8mb4' );

/** The database collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );

//...

You should see above code into your wp-config.php file. This code is for creating global constants for database settings like DB_NAME, DB_USER, etc.

Available Constants Database Settings are –

  • DB_NAME – Database name
  • DB_USER – Database user
  • DB_PASSWORD – Database password
  • DB_HOST – Database hostname
  • DB_CHARSET – Database charset when creating tables
  • DB_COLLATE – Database collate type

These above are global constants in wordpress. Constant means the values will be fixed through out entire application. It can’t be changed.


How To Use?

Here, we will consider that a custom plugin executing a shell command. To execute shell commands we generally use shell_exec() PHP function.

Example

Plugin to export database and save into a specific path.

<?php

//...

$db_user = DB_USER;
$db_pwd = DB_PASSWORD;
$db_name = DB_NAME;
$bkp_file_path = "/var/www/html/backup/" . time() . ".sql";

// create database backup 
shell_exec("mysqldump -u {$db_user} -p{$db_pwd} {$db_name} > {$bkp_file_path}");

//...

Here,

You can see inside above code we are using global settings of wordpress database into a plugin code.

We hope this article helped you to learn How To Access WordPress Database Details in Plugin Tutorial in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5