title: Wordpress Ajax Techniques
body: <p>I've been recently playing with implementing AJAX into WordPress. I know there is many plugins available but I wanted to make it on my own.</p>
  
  <p>In articles regarding to AJAXified WordPress most people recommend using <em>admin-ajax.php</em> to process AJAX requests. My first idea how to make it was to simply create custom get_header() and get_footer()</p>
  
  <h2>1st way</h2>
  
  <pre><code>// Boolean function ?ajax=true
  function is_ajax () {
    if($_REQUEST['ajax']) {
      return true;
    } else {
      return false;
    }
  }
  
  function ajax_get_header () {
    if(is_ajax()) {
      get_header('ajax'); 
      /* Insert header-ajax.php which
      includes only google analytics tracking code and some minor stuff */
      return true;
    } else {
      get_header();  
      // Standard header
      return true;
    }
  }
  
  /* Function ajax_get_footer() pretty much the same */
  </code></pre>
  
  <p>Then, page templates would look like</p>
  
  <pre><code>&lt;?php ajax_get_header(); ?&gt;
  
  &lt;!-- Content --&gt;
  
  &lt;?php ajax_get_footer(); ?&gt;
  </code></pre>
  
  <p>And making ajax calls the standard way, of course.
  This method looks for me simple and clean.
  On the other hand, many people recommend using built-in function, by creating a hook to catch AJAX calls.</p>
  
  <h2>2nd way</h2>
  
  <pre><code>function process_ajax(){
    /* Show the page or whatever */
  }
  add_action('wp_ajax_nopriv_ajax', 'process_ajax');
  add_action('wp_ajax_ajax', 'process_ajax');
  </code></pre>
  
  <p>And pointing AJAX calls to admin-ajax.php</p>
  
  <h2>Which one to use?</h2>
  
  <p>I have tried both these methods and discovered that the first way is loading remarkably faster then the latter one. In same conditions the 1st way (ajax_get_header) took approximately 400ms to load a page (almost no content) and the 2nd way (admin-ajax.php) about 800ms. I dont know why, both ways load WP core and do thing almost the same.</p>
  
  <p>So, I am asking you, is there a serious reason to make AJAX calls through admin-ajax.php? Is it nessesary? And why it takes more time to process a call through the recommended way?</p>