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);

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.

  • 1
    Please explain why exactly it's not possible as plugin.
    – kaiser
    May 4, 2012 at 7:53
  • Because the script require to be publicly accessed, not on the admin side( it will not work on any folder like wp-content/plugins since a login screen may come across ).
    – user983248
    May 4, 2012 at 10:28
  • I think you might want to edit your question to say what you want to do with your script. Pretty much anything is possible as a plug-in :) May 4, 2012 at 13:19
  • IPN validation for Paypal, See, it didn't work for me while doing it from the Plugins folder, but yes from a folder outside the whole Wordpress installation
    – user983248
    May 4, 2012 at 14:38

2 Answers 2


Using the defines the user sets in wp-config:

mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

EDIT: Since your script is outside the Wordpress environment, what you want to do is initiate it before using the defines in wp-config.

mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
  • 1
    Which you stated that it currently doesn't work in your own question. Not going to downvote, but please make shure that your answer really works and shows what the OP makes wrong. Thanks! :) Btw: Welcome to WPSE and don't let that little push by me hold you back from answering other questions. Answering is always highly appreciated.
    – kaiser
    May 4, 2012 at 7:52
  • The connection to the database works. The problem in my question is abstracting the function that calls it out to an external file. The values I'm using here are defines set in wp-config.php which you use to set up Wordpress. Did you at least try it before assuming it doesn't work?
    – akamaozu
    May 4, 2012 at 9:55
  • This is a bit off the original question
    – user983248
    May 4, 2012 at 10:44
  • Have you actually tried it? I had the exact same problem you have (connecting to the database) and I solved it by using the defines set by wp-config (like you requested). The only variable you need is $db_name, since all the rest are already in the WP environment thanks to wp-config.php. As long as the Wordpress environment is loaded, you have total access to the defines. edit: Is your script outside the Wordpress environment?
    – akamaozu
    May 4, 2012 at 10:50
  • Yes, please read my last edit, and thanks for taking the time
    – user983248
    May 4, 2012 at 11:01

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:

$posts = $wpdb->get_results("SELECT ID, post_title FROM wp_posts WHERE post_status = 'future' AND post_type='post' ORDER BY post_date ASC LIMIT 0,4");

Check out this article for additional info: http://wp.smashingmagazine.com/2011/09/21/interacting-with-the-wordpress-database/

  • 1
    When I remove the link from your answer, I got no information about what the actual solution would be, aside from a hint that $wpdb can perform basic database tasks. Would you please mind to improve your answer to show off some basic example? Thanks.
    – kaiser
    Apr 5, 2014 at 2:09
  • The article there has a very detailed description of the $wpdb object, so I didn't want to the cut and paste a lot of text there. But basically if your script is part of the WordPress, you can use the $wpdb object to run the database queries like this: $posts = $wpdb->get_results("SELECT ID, post_title FROM wp_posts WHERE post_status = 'future' AND post_type='post' ORDER BY post_date ASC LIMIT 0,4"); The person asking the question clarified it later that (s)he does not want to make it a plugin, so my answer is less relevant now, so I decided to leave it as is. Dec 11, 2014 at 0:21
  • Please always put any information one needs into the question. Comments get cleaned up regularly. Anyway, I read the other answer and the question again and -1ed both of them. By now the original question still looks like an attempt to hack/infect a site and the other answer is against best practice in every single line.
    – kaiser
    Dec 11, 2014 at 1:36
  • This is the better solution in my opinion. Making use of build-in WordPress functions is always preferable. After looking in $wpdb Object it should become clear. Jun 14, 2017 at 13:14

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