Title: Including wp-config.php
Last modified: August 18, 2016

---

# Including wp-config.php

 *  [jalenack](https://wordpress.org/support/users/jalenack/)
 * (@jalenack)
 * [21 years, 1 month ago](https://wordpress.org/support/topic/including-wp-configphp/)
 * Ok, so I’m writing a plugin for a live shoutbox / chat. It uses AJAX etc. and
   you can view a sample of it at [http://blog.jalenack.com](http://blog.jalenack.com)
 * Well now I’m trying to make it wordpress friendly by using the same database 
   and writing a new table for its info. Well I’ve got that fine but I’m having 
   trouble including the wp-config file from my plugin.
 * The plugin is located at /wp-content/plugins/***/ and it needs to include the
   config file at /wp-config.php
 * When I try it directly,
 * `Warning: main(): URL file-access is disabled in the server configuration in /****/
   blog.jalenack.com/wp-content/plugins/*** on line 3`
 * So what am I doing wrong? Do I need to include it some other way…?
 * Thanks, Andrew
 * P.S. Is anyone interested in this plugin?

Viewing 15 replies - 1 through 15 (of 15 total)

 *  Thread Starter [jalenack](https://wordpress.org/support/users/jalenack/)
 * (@jalenack)
 * [21 years, 1 month ago](https://wordpress.org/support/topic/including-wp-configphp/#post-194811)
 * Ok, let me rephrase…
 * How can plugins access the main database?
 *  [moshu](https://wordpress.org/support/users/moshu/)
 * (@moshu)
 * [21 years, 1 month ago](https://wordpress.org/support/topic/including-wp-configphp/#post-194815)
 * I guess it should be here:
    [http://codex.wordpress.org/Developer_Documentation](http://codex.wordpress.org/Developer_Documentation)
 *  [ColdForged](https://wordpress.org/support/users/coldforged/)
 * (@coldforged)
 * [21 years, 1 month ago](https://wordpress.org/support/topic/including-wp-configphp/#post-194818)
 * Did you try just accessing the database through the global `$wpdb` variable? 
   A plugin shouldn’t necessarily need to include the config file.
 *  Thread Starter [jalenack](https://wordpress.org/support/users/jalenack/)
 * (@jalenack)
 * [21 years, 1 month ago](https://wordpress.org/support/topic/including-wp-configphp/#post-194903)
 * Sorry, I’m still having a bit of trouble.
 * Here’s how it is currently set up, everything in one folder within the /wp-content/
   plugins/ folder:
 * There is the main plugin file with a single function that will print the html
   for my plugin to a page. There is nothing else in that file.
 * Next, there is a javascript file that runs the AJAX backend and calls two separate
   php files at different times, one for sending information and one for receiving
   it.
 * These separate php files need to connect to the database. Currently it uses a
   separate include file with the database login information, identical to that 
   found in the wp-config file.
    Obviously, that’s pain, and I’d like to just use
   the wp-config information.
 * The problem is, the 2 get and send php scripts are not connected with WordPress,
   so they can’t receive the global`$wpdb`. So the most convenient way would be 
   to just include the wp-config file..but I don’t think that’ll work. Should I 
   integrate some of the files, and if so, how? Any ideas on how to get the database
   info?
 *  [Joshua Sigar](https://wordpress.org/support/users/alphaoide/)
 * (@alphaoide)
 * [21 years, 1 month ago](https://wordpress.org/support/topic/including-wp-configphp/#post-194908)
 * How did you include the file?
    ` <?php require_once('/wp-config.php'); // if 
   it's in root //require_once('/blog/wp-config.php'); in a 'blog' directory ?>
 *  [ColdForged](https://wordpress.org/support/users/coldforged/)
 * (@coldforged)
 * [21 years, 1 month ago](https://wordpress.org/support/topic/including-wp-configphp/#post-194913)
 * Ah, that makes more sense. I usually do the following, with an appropriate number
   of `dirname()` functions to back out of wherever the PHP file is located to get
   back to the `wp-config.php`. this example assumes we’re in the `wp-content/plugins`
   directory.
 * `include( dirname( dirname ( __FILE__ ) ) )."/wp-config.php" );`
 *  Thread Starter [jalenack](https://wordpress.org/support/users/jalenack/)
 * (@jalenack)
 * [21 years, 1 month ago](https://wordpress.org/support/topic/including-wp-configphp/#post-194942)
 * MMmm, still not working.
 * I’ve uploaded a copy of the current files (in a .zip) at
    [http://blog.jalenack.com/live.zip](http://blog.jalenack.com/live.zip)
 * It’s relatively complete, but you’re the very first ones to see it so be careful.
   I wrote up an installation process in the readme.txt . Installation is pretty
   standard. Again, this has never been tested outside of my blog, so I’m not guaranteeing
   anything. For reference on how it _should_ work, [http://blog.jalenack.com](http://blog.jalenack.com)
   has it running.
 * Anyways, my problem continues. Using ColdForged’s code (alpahoide, I wish it 
   were that easy..that’s the first thing I tried), I managed to include it just
   fine and even managed to print the $server, $loginsql, $passsql, and $base variables
   to a blank browser. However, it still won’t work with my plugin.
 * It may be a conflict with the inclusion of other files from wp-config.php, such
   as wp-blog-header.php
 * I think I may need to just restructure somehow, so that I can use the $wpdb variable
   in some way. Thoughts?
 *  [Joshua Sigar](https://wordpress.org/support/users/alphaoide/)
 * (@alphaoide)
 * [21 years, 1 month ago](https://wordpress.org/support/topic/including-wp-configphp/#post-194953)
 * If it’s a plugin, then it should use $wpdb.
    I still don’t get whether you couldn’t
   figure out how to access $wpdb or access to $wpdb is indeed not possible, which
   I couldn’t figure out.
 *  Thread Starter [jalenack](https://wordpress.org/support/users/jalenack/)
 * (@jalenack)
 * [21 years, 1 month ago](https://wordpress.org/support/topic/including-wp-configphp/#post-194954)
 * I haven’t figured out how to access the $wpdb variable…
 *  [Joshua Sigar](https://wordpress.org/support/users/alphaoide/)
 * (@alphaoide)
 * [21 years, 1 month ago](https://wordpress.org/support/topic/including-wp-configphp/#post-194955)
 * basically in your function
    ` function my_func() { global $wpdb;
 * $query = "SELECT bla..bla";
    $result = $wpdb->get_results( $query); ... $query
   = "INSERT INTO ..."; $wpdb->query( $query ); }
 * It’s time to reverse engineering other available plugins.
 *  Thread Starter [jalenack](https://wordpress.org/support/users/jalenack/)
 * (@jalenack)
 * [21 years, 1 month ago](https://wordpress.org/support/topic/including-wp-configphp/#post-195116)
 * Alright, I’ve made some progress. I’ve managed to move everything into one big
   file that is activated in the plugin and that works great…so I can now use the`
   $wpdb` variable without problems….err…I can now receive the $wpdb, whereas before
   I couldn’t. Now, I’m still struggling with how it works. I’ve spent a lot of 
   time looking at other plugins, but I’m still not getting it. Below is an example
   of how it is set up now…
 * `// establishes a connection to a mySQL Database accroding to the details specified
   in db.php
    function getDBConnection () { include("db.php" ); // contains the 
   variables below >:-( $conn = mysql_connect($server, $loginsql, $passsql); if (!
   $conn) { echo "Connection to DB was not possible!"; end; } if (!mysql_select_db(
   $base, $conn)) { echo "No DB with that name seems to exist at the server!"; end;}
   return $conn; }
 * // retrieves all messages with an id greater than $lastID
    function getData($
   lastID) { $sql = "SELECT * FROM wp_liveshoutbox WHERE id > ".$lastID." ORDER 
   BY id ASC LIMIT 60"; $conn = getDBConnection(); $results = mysql_query($sql, 
   $conn); if (!$results || empty($results)) { //echo 'There was an error creating
   the entry'; end; } while ($row = mysql_fetch_array($results)) { //the result 
   is converted from the db setup (see initDB.php) $name = $row[2]; $text = $row[
   3]; $id = $row[0]; if ($name == '') { $name = 'no name'; } if ($text == '') {
   $name = 'no message'; } echo $id." ---".$name." ---".$text." ---"; // --- is 
   being used to separete the fields in the output } }
 * Thank you all so much for your help
 *  [Joshua Sigar](https://wordpress.org/support/users/alphaoide/)
 * (@alphaoide)
 * [21 years, 1 month ago](https://wordpress.org/support/topic/including-wp-configphp/#post-195117)
 * You don’t need getDBConnection () at all. Also how do you determine the table
   name in the installation phase of plugin? Did you hardcode “wp_liveshoutbox”?(
   You know where I’m getting at)
 * `
    function getData($lastID) { global $wpdb
 * `$sql = "SELECT * FROM wp_liveshoutbox WHERE id > ".$lastID." ORDER BY id ASC
   LIMIT 60";`
 * `$results = $wpdb->get_results( $sql);
    if (!$results || empty($results)) { //
   echo 'There was an error creating the entry'; end; }
 * `foreach( $results as $r ) {
    // assuming you were fetching from // fields: name,
   text, and id $name = $r->name; $text = $r->text; $id = $r->id; if ($name == ''){
   $name = 'no name'; } if ($text == '') { $name = 'no message'; } echo $id." ---".
   $name." ---".$text." ---"; // --- is being used to separete the fields in the
   output } }
 *  Thread Starter [jalenack](https://wordpress.org/support/users/jalenack/)
 * (@jalenack)
 * [21 years, 1 month ago](https://wordpress.org/support/topic/including-wp-configphp/#post-195190)
 * Thank you alphaoide, that worked well with a couple touchups. Yes, I hardcoded
   the database name on creation of the DB…but I will correct that in the real release.
 * The really weird thing is it still doesn’t work. I tried both the old way and
   the new (with $wpdb) way without enclosing them in functions (making them print
   to the top of the page), and both outputted EXACTLY the same text.
 * Here is the old function:
 * `function getData($lastID) {
    $sql = "SELECT * FROM wp_liveshoutbox WHERE id 
   > ".$lastID." ORDER BY id ASC LIMIT 60"; $conn = getDBConnection(); $results 
   = mysql_query($sql, $conn); while ($row = mysql_fetch_array($results)) { $chat_user_name
   = $row[2]; $chat_user_text = $row[3]; $chat_user_id = $row[0]; echo $chat_user_id."---".
   $chat_user_name." ---".$chat_user_text." ---"; // --- is being used to separate
   the fields in the output }
 * }
 * And here is the new one.
 * `
    function getData($lastID) { global $wpdb; $sql = "SELECT * FROM wp_liveshoutbox
   ORDER BY id ASC LIMIT 60"; $results = $wpdb->get_results( $sql);
 *  foreach( $results as $r ) {
    $chat_user_name = $r->name; $chat_user_text = $
   r->text; $chat_user_id = $r->id; echo $chat_user_id." ---".$chat_user_name." ---".
   $chat_user_text." ---"; // --- is being used to separate the fields in the output}}
 * So it shouldn’t be a problem with that part of it.
 * Here’s the other part of the script that deals with this function:
 * `if ($javascript == "yes") { getData($lastID); }`
 * and part of the javascript file:
 * `var GetChaturl = "/wp-content/plugins/live-shoutbox/live-shoutbox.php?javascript
   =yes";`
 * `function receiveChatText() {
    if (httpReceiveChat.readyState == 4 || httpReceiveChat.
   readyState == 0) { httpReceiveChat.open("GET",GetChaturl + '&lastID=' + lastID
   + '&rand='+Math.floor(Math.random() * 1000000), true); httpReceiveChat.onreadystatechange
   = handlehHttpReceiveChat;  httpReceiveChat.send(null); } }
 * It’s very strange considering both functions print the same thing, but when you
   switch the old for the new, it doesn’t work. Any ideas?
 *  [raynerape](https://wordpress.org/support/users/raynerape/)
 * (@raynerape)
 * [21 years, 1 month ago](https://wordpress.org/support/topic/including-wp-configphp/#post-195191)
 * I am not developer, but from the demo I’ve seen on your website, this plugin 
   is marvelous. I can’t wait for the day I can implement this shoutbox on my website!
 *  Thread Starter [jalenack](https://wordpress.org/support/users/jalenack/)
 * (@jalenack)
 * [21 years, 1 month ago](https://wordpress.org/support/topic/including-wp-configphp/#post-195193)
 * Muahaha, I have completed it. I took a break to go outside and get some fresh
   city air when it came the solution came to me. See, I hadn’t realized that when
   the javascript file called the plugin, it was directly including it, and not 
   going through wordpress. This had the effect of rendering $wpdb useless. So I
   managed to include wp-config.php after all. So, all systems are go now. I will
   definitely be releasing it this week, if not tomorrow. Thanks!
 * Well now, the only thing I have left to work out is the hardcoded “wp_liveshoutbox”.
   So when I create the table, I have to create $wpdb->liveshoutbox , right? And
   then call it with the same thing. Is there anything I’ve missed?
 * Right now if I try to call it with $wpdb->liveshoutbox nothing happens. I’m using
   the default table prefix and all, any ideas?

Viewing 15 replies - 1 through 15 (of 15 total)

The topic ‘Including wp-config.php’ is closed to new replies.

 * 15 replies
 * 5 participants
 * Last reply from: [jalenack](https://wordpress.org/support/users/jalenack/)
 * Last activity: [21 years, 1 month ago](https://wordpress.org/support/topic/including-wp-configphp/#post-195193)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
