Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Trying to figure out an issue a fellow programmer is having. I was wondering if the functions.php file get called at all when you do admin side AJAX? I know that when you do an AJAX call a part of WP gets loaded up to process the call and send back a response. Is the functions.php file included in that?

The reason I'm asking is because he's using the class from the Meta-Box` plugin and loading it as part of a theme instead. There is some AJAX in that class that only returns empty responses and I think its because the code that handles the response doesn't get loaded. Is there any documentation to what gets loaded when WP handles AJAX?

share|improve this question
add comment

2 Answers

up vote 18 down vote accepted

admin-ajax.php loads wp-load.php:

/** Load WordPress Bootstrap */
require_once( dirname( dirname( __FILE__ ) ) . '/wp-load.php' );

wp-load.php loads wp-config.php, and there wp-settings.php is loaded.

And here we find this:

// Load the functions for the active theme, for both parent and child theme if applicable.
if ( ! defined( 'WP_INSTALLING' ) || 'wp-activate.php' === $pagenow ) {
    if ( TEMPLATEPATH !== STYLESHEETPATH && file_exists( STYLESHEETPATH . '/functions.php' ) )
        include( STYLESHEETPATH . '/functions.php' );
    if ( file_exists( TEMPLATEPATH . '/functions.php' ) )
        include( TEMPLATEPATH . '/functions.php' );
}

So, yes, the theme’s functions.php is loaded.


There is one exception in wp-settings.php:

// Stop most of WordPress from being loaded if we just want the basics.
if ( SHORTINIT )
    return false;

When SHORTINIT is defined as TRUE earlier (by a plugin, mu-plugin or a drop-in), the theme will not be loaded.

One of the many reasons why custom post types and taxonomies break when defined in a theme.

So check if SHORTINIT is TRUE for some reason.


Another common error is the wrong usage of is_admin(). This is always TRUE in admin-ajax.php, so the following will fail:

if ( ! is_admin() )
    // register or execute AJAX stuff

Debugging AJAX

One method as primitive as efficient is using HTTP header to debug AJAX.

Here is a simple helper function:

function send_debug_header( $msg )
{
    static $counter = 1;
    header( "X-Debug-Ajax-$counter: $msg" );
    $counter += 1;
}

And this plugin shows how to use it:

<?php # -*- coding: utf-8 -*-
/**
 * Plugin Name: Debug AJAX per HTTP
 * Description: Look at the HTTP headers in your browser's network console
 */

// The constant is already defined when plugins are loaded.
// Prove we have been called.
if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
    send_debug_header( 'File "' . __FILE__ . '" was called on an AJAX request.' );

function send_debug_header( $msg )
{
    static $counter = 1;
    header( "X-Debug-Ajax-$counter: $msg" );
    $counter += 1;
}

add_action( 'wp_ajax_debug_test',        't5_debug_test' );
add_action( 'wp_ajax_nopriv_debug_test', 't5_debug_test' );

function t5_debug_test()
{
    $in = is_user_logged_in() ? '' : 'not ';
    send_debug_header( 'Function "' . __FUNCTION__ . '" was called and the user is ' . $in . 'logged in.' );
    print_r( debug_backtrace() );
    die(1);
}

add_action( 'wp_enqueue_scripts', 't5_enqueue_jquery' );

function t5_enqueue_jquery()
{
    wp_enqueue_script( 'jquery' );
}
add_action( 'wp_footer', 't5_debug_ajax_test_button', 0 );

function t5_debug_ajax_test_button()
{
    ?>
<input type="submit" id="t5debugajax" value="Debug AJAX">
<script>
jQuery( function($){
    var sendFeedBack = function( response ){
        console.log( response );
    };
    $("#t5debugajax").on("click", function(){
        $.post(
            "<?php echo admin_url( 'admin-ajax.php' ); ?>",
            {
                action: "debug_test"
            },
            sendFeedBack
        );
    });
});
</script>
    <?php
}

It will add a button to the front end that triggers an AJAX request when clicked. The open your browser’s network console and look at the response headers for the request:

enter image description here

share|improve this answer
    
Thank you for the help. Trying to debug this is getting tricky –  Manny Fleurmond Apr 20 '13 at 13:03
    
@MannyFleurmond See my updates for possible problems. –  toscho Apr 20 '13 at 13:12
    
As detailed as ever, @toscho. Its especially hard to debug when the code runs fine on your end but not for someone else. Can't seem to reproduce the problem but your answer will probably send me in the right direction. –  Manny Fleurmond Apr 20 '13 at 13:15
    
@MannyFleurmond I have added a debug helper plugin. That should help finding the problem. –  toscho Apr 20 '13 at 13:46
7  
Man, you are thorough :) –  Manny Fleurmond Apr 20 '13 at 13:58
show 1 more comment

I assume your problem was AJAX was working if your are logged in and it was not working in logged out status, right?
There is a function in WordPress to access AJAX-based files for non logged users: wp_ajax_nopriv, for example

/* works for logged users */
add_action( 'wp_ajax_my_action', 'my_action_callback');

/* works for non logged users */
add_action( 'wp_ajax_nopriv_my_action', 'my_action_callback');
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.