WordPress stores all the website data in a database and uses it, for example, if a certain content or settings of a post will output to a visitor. In most cases, a MySQL database is used.
As theme or plugin developers there may be reasons to change specific settings in the WordPress database. The following example shows how to connect to the assigned MySQL database.
How can I connect to the WordPress MySQL database?
The following PHP function uses the WordPress constants “DB_HOST“, “DB_USER“, “DB_PASSWORD” and “DB_NAME“, which are already defined in the “wp-config.php” file of WordPress.
How do we use this function?
Since we were able to successfully connect to the MySQL database, we can now use the PHP function above to setup certain changes to the WordPress database.
The following small snippet shows an example of how we can rename an old MySQL table name to a new one.