WordPress.org

Ready to get started?Download WordPress

Forums

Including wp-config.php (16 posts)

  1. jalenack
    Member
    Posted 9 years ago #

    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

    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?

  2. jalenack
    Member
    Posted 9 years ago #

    Ok, let me rephrase...

    How can plugins access the main database?

  3. moshu
    Member
    Posted 9 years ago #

  4. ColdForged
    Member
    Posted 9 years ago #

    Did you try just accessing the database through the global $wpdb variable? A plugin shouldn't necessarily need to include the config file.

  5. jalenack
    Member
    Posted 9 years ago #

    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?

  6. Joshua Sigar
    Member
    Posted 9 years ago #

    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
    ?>

  7. ColdForged
    Member
    Posted 9 years ago #

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

  8. jalenack
    Member
    Posted 9 years ago #

    MMmm, still not working.

    I've uploaded a copy of the current files (in a .zip) at
    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 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?

  9. Joshua Sigar
    Member
    Posted 9 years ago #

    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.

  10. jalenack
    Member
    Posted 9 years ago #

    I haven't figured out how to access the $wpdb variable...

  11. Joshua Sigar
    Member
    Posted 9 years ago #

    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.

  12. jalenack
    Member
    Posted 9 years ago #

    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

  13. Joshua Sigar
    Member
    Posted 9 years ago #

    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
    }
    }

  14. jalenack
    Member
    Posted 9 years ago #

    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?

  15. RaynerApe
    Member
    Posted 9 years ago #

    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!

  16. jalenack
    Member
    Posted 9 years ago #

    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?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags