[wp-hackers] "deprecating" direct calls to wp-config.php

Otto otto at ottodestruct.com
Tue Nov 17 16:37:01 UTC 2009


Have you read the codex article? I thought it was perfectly clear.
http://codex.wordpress.org/AJAX_in_Plugins

I'll try to as simple as possible here:

1. You have some code that you want to call via an AJAX request. That
code will go in a plugin, like this:

add_action('wp_ajax_whatever', 'whatever_callback');
function whatever_callback() {
// do anything you like here
// check $_POST, whatever you want, All WordPress is available to you
// echo out whatever you like to return back to the AJAX call, then
die();
}

That is the server side code. That is what you will be calling. That's
what replaces the need for you to load wp-config.php at all.

The trick to this is that you won't call yourfile.php directly from
your javascript any more. Instead, your javascript will call the
admin-ajax.php file. The exact URL your javascript needs to call can
be generated using admin_url("admin-ajax.php"). So, when you're
outputting your javascript (and this can be anywhere you like, mind
you, not just in admin_header), then you'll use that as the point to
which you make the request.

For example, in jQuery, you'd do something like this:

jQuery(document).ready(function($) {
	var data = {
		action: 'whatever',
		anything: 'you like here'
	};
	jQuery.post(<?php echo admin_url("admin-ajax.php"); ?>, data,
function(response) {
		alert('Got this from the server: ' + response);
	});
});


See? It's making a POST request to the admin-ajax file. The "action"
of "whatever" is what triggers that call to the wp_ajax_whatever
action hook, which then runs your server side code within the
WordPress context.

No need for calling wp-config.php anywhere.

-Otto



On Tue, Nov 17, 2009 at 10:29 AM, Olivier <autremonde75 at gmail.com> wrote:
> Otto,
>
> Thanks for your prompt answer! I don't really understand your
> statment :
> "The place where you load wp-config.php can be replaced by putting
> your code in an action triggered by wp_ajax_whatever. That's the
> difference."
> I guess you are talking about some kind of action hook like
> add_action('admin_header','my_ajax_function');
> Right? Do I place this action hook in the same file as the ajax
> function, no matter where is the hook?
>
> How do I link my include ajax function code into the plugin? By
> enqueuing the script through an admin_print_scripts callback function?
> Or rather by a require_once in the plugin master file?
>
> Sorry if this is obvious for you but it is really not for. I have
> tried several things and I am not able to find a solution to remove
> the wp-config.php :(
>
> Eric,
>
> Thanks as well for your point. The admin_url is a function that
> requires the wp-config.php to be loaded (until I find a solution to
> bypass it).
>
> Thanks again!
>
> Olivier
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list