WordPress.org

Support

Support » Plugins and Hacks » [Resolved] Using $wpdb outside wordpress

[Resolved] Using $wpdb outside wordpress

  • I’m writing a plugin, but a AJAX request is calling a php file and I want to create tables and save data that have the same prefix as the other WordPress tables.

    The problem is you can NOT just global $wpdb and have it work, because no other files are involved! What file should I include so that the $wpdb could be global?

Viewing 15 replies - 1 through 15 (of 18 total)
  • Probably wp-includes/wp-db.php.

    whooami

    @whooami

    Member

    whenever you want to use wordpress fucntions OR variables outside of wordpress, you need to include wp-blog-header.php

    How do you include it? I know it’s in the root, but how do I reference it? I tried many things, but it either didn’t work or gave me an access denied error.

    All I want is to be able to reference the WPDB value in my AJAX handler file.

    whooami

    @whooami

    Member

    How do you include it?

    http://us.php.net/include/

    there are hundreds, if not thousands, of examples of php includes inside all of the wordpress files themselves.

    Actually, thinking about that again the devs tend to use requires but the format is identical. 🙂

    To be specific, I have a template that calls an AJAX function and loads another file (which isn’t a template, just a PHP file). How can I have WordPress functions and variables available in that file?

    Whooami, I used include, but it gave me an error. Also, how do I reference it? I can just use “/wp-blog-header.php” for the one in root, but what about when the wordpress install isn’t in the root? How do I find it or does it just have to be hard-coded?

    whooami

    @whooami

    Member

    did you look at the link I provided?

    You reference it, using the path to it, from the file you are including it in.

    do we need a primer on paths?

    ./ == current directory
    ../ == one directory up
    ../stuff == one directory up and then into stuff.

    ../../ == 2 directories up.

    ../../morestuff == 2 directories up and then into morestuff

    Error:
    Warning: require(./wp-blog-header.php) [function.require]: failed to open stream: No such file or directory

    whooami

    @whooami

    Member

    ./things == inside the current directory and then into the subdirectory named things.

    If you stop pasting, and typing and start reading ..

    Hmm… I suppose the themes are always the same depth… I just don’t want to make it difficult for people who use my code and have non-standard installations. So I suppose I’ll have to use require(“../../../wp-blog-header.php);

    (using that, it’s working. As long as no one is putting their theme and deeper or more shallow than normal, I guess it will work).

    Thanks!

    whooami

    @whooami

    Member

    if you are doing this for a plugin, there are other options. there are all kinds of preassigned variables that could handle simplifying that.

    Not a plugin in this case. Actual file here:

    http://www.jeremyduffy.com/advanced-blog-search/

    Does anyone know how I would go about doing this now that the wp-content directory can be moved if the blog isn’t in the site root?

    Nevermind: I wound up just passing the ABSPATH constant through the ajax request as a var. Then I included the file I needed.

    mysack.setVar( "wp_root" , "<?php echo ABSPATH;?>" );

    … Ugh.

    If you have to have an external file include the WP functions because you’re referencing them directly like that, then you’re pretty much doing it wrong.

    Consider alternate approaches, like passing the actual data you need to the files directly, or generating the files on the fly from the plugin, or some such thing.

    Because every time you include the entirety of WordPress again when only one page is being viewed, you increase the server load by double. And God kills a kitten.

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘[Resolved] Using $wpdb outside wordpress’ is closed to new replies.
Skip to toolbar