[wp-hackers] ajax and $wpdb

Silverstein, Jesse Jesse.Silverstein at xerox.com
Wed Jul 29 18:45:56 UTC 2009

Re: Simon's problem of plugin directory location & editing plugin files:

I think if your plugin file has loaded, it's safe to assume that ABSPATH has been defined, and you can go from there.
For example:
require_once ABSPATH . WPINC . "/functions.php";
or something along those lines. Dynamically reference the core files you need based on the global definitions already available to you. That way there is no plugin editing required, and your plugin can work regardless of where the content directory is or where the plugins directory is, etc.

When referencing other files in your plugin directory, (and please correct me if I'm wrong here,) it's best to use WP_PLUGIN_URL or WP_PLUGIN_DIR, depending on the use case (URL for anything that gets printed to the browser or for redirects, DIR for includes and requires). For backward compatibility to 2.5ish,

if ( !defined( 'WP_CONTENT_URL' ) )
	define( 'WP_CONTENT_URL', get_option( 'site_url' ) . '/wp-content' );
if ( !defined( 'WP_CONTENT_DIR' ) )
	define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );
if ( !defined( 'WP_PLUGIN_URL' ) )
	define( 'WP_PLUGIN_URL', WP_CONTENT_URL . '/plugins' );
if ( !defined( 'WP_PLUGIN_DIR' ) )
	define( 'WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins' );

I can't take credit for that code because I think I found in on the codex somewhere. Regardless, I hope it helps someone out there.


-----Original Message-----
From: wp-hackers-bounces at lists.automattic.com [mailto:wp-hackers-bounces at lists.automattic.com] On Behalf Of Simon Wheatley
Sent: Wednesday, July 29, 2009 2:05 PM
To: wp-hackers at lists.automattic.com
Subject: Re: [wp-hackers] ajax and $wpdb

Others have answered the AJAX question, but for completeness
wp-load.php is what you wanted rather tha wp-blog-header.php.

However to add my tuppenny-worth, and this is why the AJAX solutions
mentioned are more cleverer, you can't guarantee the relative location
of wp-load.php to your plugin files (they might have moved wp-content
outside the main WP files, etc, etc)... so to get that to work you'd
have to have someone edit the plugin files to specify the location...
and then when someone uses the nifty cool "update plugin" feature all
the edits would be overwritten and the plugin breaks. This last point
is why we all need to drill "never edit plugin files" into people as
thoroughly as we've been drilling "never hack core".


On Wed, Jul 29, 2009 at 11:26 AM, Adam Taylor<adamjctaylor at gmail.com> wrote:
> Hi All,
> I'm using AJAX to call my plugin script to process data and update what is
> displayed (essentially a thumbs up/thumbs down voting plugin).  This is
> fine, I can update the text but I need to also interact with the database.
> As I understand, because I am calling my script directly not from within
> wordpress $wpdb is not available.
> I read that you can include wp-blog-header.php and everything should be
> available and have added - require_once('../../../wp-blog-header.php');  -
> to the function requiring it, however, I get the following error:
> PHP Fatal error:  Call to a member function set_prefix() on a non-object in
> /dev/site/wp-settings.php on line 255
> Firstly: what's going on here and how can I fix it?
> Secondly: is this the best way of doing this? I can't find any tutorials or
> documents about writing an ajaxy plugin for wordpress that interacts with
> the database. This topic http://wordpress.org/support/topic/184095 suggests
> that it is not but I don't understand the suggested solutions.
> Thanks in advance,
> Adam

Sweet Interaction Ltd is Registered in England/Wales, no. 6610741
Registered office: 7 Malton Av, Manchester, M21 8AT
wp-hackers mailing list
wp-hackers at lists.automattic.com

No virus found in this incoming message.
Checked by AVG - www.avg.com 
Version: 8.5.392 / Virus Database: 270.13.35/2270 - Release Date: 07/29/09 06:12:00

More information about the wp-hackers mailing list