Disable Ajax for Spiders

Here’s a programatic method. What it does it checks if an ajax request is running and if the useragent to see if it is in the defined whitelist and if it is then it continues loading wp as normal, else it dies. I used registered_taxonomy because it’s the second hook in the hook loading processes:

http://codex.wordpress.org/Plugin_API/Action_Reference

I’m going to add it into the themes functions.php and test:

/* Disable AJAX for Spiders */
add_action( 'registered_taxonomy' , 'disable_ajax_for_spiders' );

function disable_ajax_for_spiders() {

    if ( !defined('DOING_AJAX') || !DOING_AJAX ) {
        return;
    }

    if ( !isset($_SERVER['HTTP_USER_AGENT']) )  {
        return;
    }

    $visitor_useragent = strtolower($_SERVER['HTTP_USER_AGENT']);

    $useragents[] = 'msie';
    $useragents[] = 'firefox';
    $useragents[] = 'webkit';
    $useragents[] = 'opera';
    $useragents[] = 'netscape';
    $useragents[] = 'konqueror';
    $useragents[] = 'gecko';
    $useragents[] = 'chrome';
    $useragents[] = 'songbird';
    $useragents[] = 'seamonkey';
    $useragents[] = 'flock';
    $useragents[] = 'AppleWebKit';
    $useragents[] = 'Android';
    $useragents[] = 'Lynx';
    $useragents[] = 'Dillo';

    /* If useragent in list then bail - else die() immediately*/ 
    if ($visitor_useragent)
    {
        foreach ($useragents as $k=>$useragent)
        {
            $useragent = trim($useragent);
            if ($useragent)
            {
                if(stristr($visitor_useragent, $useragent)||$useragent=='*')
                {
                    return;
                }
            }
        }
    }

    die();
}