I am learning Plugins and I am trying to connect WordPress database wp-config.php in my Plugin PHP code. Currently for checking my code, I used the below connection and it is working fine. Now I converted it into a WordPress plugin so anyone can use this but now its time to make WordPress database connection.

//Connection To DataBase
$username = "XXXXXXXXXX";
$password = "XXXXXXXXXX";
$dbname = "XXXXXXXXXXXX";
$connection = mysql_connect($host, $username, $password);

So How to insert wp-config.php like something require_once(wp-config.php'); in plugin instead of above code to make WordPress SQL database connection in my plugin file?

  • 1
    If you want to make plugin in wordpress then you dont need to connect with database like we do in PHP. For example, you can check out default plugin of wordpress Hello World 's code. Commented Sep 26, 2014 at 8:39
  • 1
    You don't want any connection while doing wordpress plugin. just use " global $wpdb;" and then use that $wpdb to create table,insert,update and all..
    – Zammuuz
    Commented Sep 26, 2014 at 9:32
  • @Rohil_PHPBeginner Can you mention here what I ask...??? Commented Sep 26, 2014 at 10:27
  • @Ammu Nice tip but I am new so can you share the basic queries of $wpdb; to INSERT,UPDATE,DELETE,WHERE like $sql= "SELECT ID FROM wp_posts WHERE post_name = 'hello-world'"; in the answer... Commented Sep 26, 2014 at 10:30
  • Read whole page to get idea about $wpdb . Commented Sep 26, 2014 at 10:50

1 Answer 1


I found the answer my self. First open your wp-config.php and check the bottom of file that Is that contain the below code?...

if ( !defined('ABSPATH') )
    define('ABSPATH', dirname(__FILE__) . '/');

If yes then add the below code to make the connection in your plugin PHP files to connect with wp-config.php file that contain Database Name, Database UserName, Database Password, Database Host... ...

require_once(ABSPATH . 'wp-config.php');
$connection = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysqli_select_db($connection, DB_NAME);

And you will get connection to WordPress database...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.