WordPress Coding

How To Connect Database in WordPress Plugin – The Beginner’s Guide

8 April, 2019 Hozaifa Sheikh 338 Views
Connect Database in WordPress Plugin

If you are WordPress codder and trying to develop a custom built plugin to extend the WordPress functionally and facing database connectivity, then this article really will help for you, here I would explain about WordPress database connection with two different database object.

How To Connect Database in WordPress Plugin for MySql?

To make a connection with the WordPress database first we need to ensure we have properly included the WordPress config files which exist into the WordPress root directory.

Step to connect with the database to MySQL:

  • First, include wp-config.php file into WordPress plugin pages
  • Make sure wp-config has included successfully
  • Now use mysql_connect, mysql_select_db function use to create the connection with WordPress database
  •  Require_once( ABSPATH . ‘wp-config.php’);
  • mysql_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

How To Connect Database in WordPress Plugin for MySqli?

MySqli is the latest version of the database which is based on object-oriented programming which is far more better than MySQL and fully optimized, so by the time start learning mySqli because since later every web application would start using MySqli instead of MySQL.

Steps Connect Database in WordPress Plugin for MySqli :

  • Include require_once( ABSPATH . ‘wp-config.php’);
  • mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  • Successfully connect with wordpress database

Anything else drop a comment please!

About author

Hozaifa Sheikh

I am a professional blogger from my heart, I am going through all the possible ways that can help to audience. I am trying my best to write content that my readers will like me. I will really be happy when my readers will become more happy with my blog.

You may also like

Leave a Reply

Notify of
Scroll to Top
Share via
Copy link
Powered by Social Snap