How can I connect to the database using the wp-config.php file ?
I’m trying to make a script more WordPress friendly, and I need to connect to the database, but without installing the script as a plugin.
Basically I have on my script
$host = "xxxxxxxxxxx"; //database location
$user = "xxxxxxxxxxx"; //database username
$pass = "xxxxxxxxxxx"; //database password
$db_name = "xxxxxxxx"; //database name
//Database Connection
$link = mysql_connect($host, $user, $pass);
mysql_select_db($db_name);
The script can not be installed as a plugin (which can make things more easy), so I need to connect to the database using the existing wp-config.php on the installation… Any ideas ???
Thanks in advance
Edit and Clarification
1- I need to use wp-config.php as it is, no modifications.
2- The script will be located at www.example.com/script/
3- It can’t be done as a plugin since the core of the script require to be publicly accessed without any login screen jumping around.
4- My question basically is how to connect to the database using the wp-config.php file by modifying the script above.
Using the defines the user sets in wp-config:
EDIT:
Since your script is outside the WordPress environment, what you want to do is initiate it before using the defines in wp-config.
You can make your script a part of your WordPress post, just use the
$wpdb
object provided by the WordPress itself. The$wpdb
object already has the database connection established and you can use it to perform any database operation: insert, update, query etc… This is preferable method for doing you DB stuff inside WordPress as you do not have to open any additional database connections.Here is a simple example for getting the future posts for instance:
Check out this article for additional info: http://wp.smashingmagazine.com/2011/09/21/interacting-with-the-wordpress-database/