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

Jason Benesch jason at realestatetomato.com
Tue Nov 3 17:53:40 UTC 2009

Wordpress now has a 'wp_ajax_nopriv_my_action' in 2.8 for your ajax calls
but following up with what Otto wrote about the template_redirect, here is a
quick example of a way you could go about making ajax requests without
having to call wp-load directly from one of your external script files.
Someone please correct me if you see anything off:

Let's say your plugin name is incredible ajax or inc_ajax

// Create some extra variables to accept when passed through the url
function my_query_vars( $query_vars ) {
    $myvars = array( 'inc_ajax' );
    $query_vars = array_merge( $query_vars, $myvars );
    return $query_vars;
add_filter( 'query_vars', 'my_query_vars' );

// Flush your rewrite rules if you want pretty permalinks
function flush_rewrite_rules() {
    global $wp_rewrite;
add_action( 'init', 'flush_rewrite_rules' );

// Create a rewrite rule if you want pretty permalinks
function add_rewrite_rules( $wp_rewrite ) {
    $wp_rewrite->add_rewrite_tag( "%inc_ajax%", "(.+?)", "inc_ajax=" );

    $urls = array( 'ajax/%inc_ajax%' );
    foreach( $urls as $url ) {
        $rule = $wp_rewrite->generate_rewrite_rules($url, EP_NONE, false,
false, false, false, false);
        $wp_rewrite->rules = array_merge( $rule, $wp_rewrite->rules );
    return $wp_rewrite;
add_action( 'generate_rewrite_rules', 'add_rewrite_rules' );

// Let's echo out the content we are looking to dynamically grab before we
load any template files
function ajax_template() {
    global $wp, $wpdb;
    if( isset( $wp->query_vars['inc_ajax'] ) && !empty(
$wp->query_vars['inc_ajax'] ) ) {
        switch( $wp->query_vars['inc_ajax'] ) {
            case 'all_users':
                $all_users = $wpdb->get_results("SELECT * FROM
                echo json_encode($all_users);
            case 'some_users':
                $some_users = $wpdb->get_results( $wpdb->prepare( "SELECT *
FROM {$wpdb->users} WHERE id = %s", $_POST['user_id'] ) );
                echo json_encode($some_users);
                echo 'You really shouldn't be here...';
add_action( 'template_redirect', 'ajax_template' );

So now from your theme file, here is an example of an ajax request using

jQuery(function($) {
        $.post( "http://mywpdomain.com/ajax/all_users",
            function( data ) {
            }, json );

My point being, you now have created an ajax request url for your blog.

You can obviously elaborate on this and you should probably use the WP
'wp_ajax_nopriv_my_action' action hook in 2.8...

But this is just an example of how you could avoid using loading the
wp-load, or wp-config from external scripts in your plugin.

Hope it helps sombody!

On Tue, Nov 3, 2009 at 9:05 AM, Otto <otto at ottodestruct.com> wrote:

> On Tue, Nov 3, 2009 at 9:43 AM, Claudio Simeone <mrbrog at gmail.com> wrote:
> > what's the reason, in details, of discouraging direct calls of
> > wp-config.php (or similar of wp-load.php)?
> > Sometimes I include wp-load.php in scripts external to WP, so I can
> > use e.g., database functions etc in those scripts.
> > Is that correct, or is to be discouraged, and if so, what is the
> > correct way to use WP environment in external scripts?
> The correct way depends on what exactly you're doing. However, there's
> never any real reason to include wp-config or wp-load directly. Doing
> so is.. not wrong, exactly... just a bit sloppy. It's error-prone,
> more likely to break things.
> If you're doing AJAX like requests and need access to WordPress
> functions, then you could use techniques similar to these:
> http://codex.wordpress.org/AJAX_in_Plugins
> For most anything else, hooking into the template_redirect function to
> produce your custom output is the better way to go.
> -Otto
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers

Jason Benesch

Real Estate Tomato
(619) 770-1950
jason at realestatetomato.com

Owner, Founder
(619) 955-7465
jason at listingpress.com

More information about the wp-hackers mailing list