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.

I have just hit my first serious issue with WordPress and for someone that enjoys Ajax this is a biggy.

I have an Ajax request that is taking 1.5 seconds to complete while using the Ajax API.

If I take the same exact code and run it with a custom script(no WordPress) the Ajax request takes merely 150 milliseconds. This is not an exaggeration

If you look at the very first comment of http://wp.smashingmagazine.com/2011/10/18/how-to-use-ajax-in-wordpress/ and the conversation that follows you will see that this slowness is caused by the fact that on your request, all of WP is loaded...

I am hoping that there is a solution out there that will make it possible to make Ajax requests while not loading all of WordPress.

What are your experiences with speeding up Ajax requests with WordPress?

share|improve this question
3  
I wonder wether popular caching plugins cover this situation. –  Raphael Feb 9 '12 at 21:12
    
@Raphael, I thought about that too, but have not seen any mention of it. That would be GREAT if they do –  Mike Feb 9 '12 at 21:25

2 Answers 2

up vote 37 down vote accepted
+50

Yep, this is nasty issue that to have full WordPress environment you need to spend considerable time loading it.

I've needed much better performance (for very dynamic incremental search feature) for work and what I went with is:

  1. Custom file as Ajax handler.
  2. SHORTINIT constant for limited WP core load.
  3. Very selectively loaded parts of core, only those needed for the task.

This provides very limited environment, but performance is way way better and reasonable degree of compatibility with WP (starting with $wpdb) is retained.

Here is start my loader file, not pretty but works for specific needs:

<?php

ini_set('html_errors', 0);
define('SHORTINIT', true);

require '../../../../wp-load.php';
require( ABSPATH . WPINC . '/formatting.php' );
require( ABSPATH . WPINC . '/meta.php' );
require( ABSPATH . WPINC . '/post.php' );
wp_plugin_directory_constants();

// stuff goes here
share|improve this answer
1  
What do you mean by SHORTINIT contant? Can you provide examples? I envision that I will need to set up my own handlers with varying degrees of WP loaded depending on the need of the request, but I would like to see some examples you have created. –  Mike Feb 9 '12 at 20:20
6  
@Mike it's not widely known but really simple in concept - if SHORTINIT constant is set WP would not load most of the core (no most of APIs/functions, no plugins, no theme). I will add some code to answer. –  Rarst Feb 9 '12 at 21:38
1  
That looks ok. I just dont like the fact that we have to use require '../../../../wp-load.php'; that makes this pretty custom. I also worry how easy it is to actually pull in the resources you "need", b/c from my experience WordPress is not very modular. –  Mike Feb 10 '12 at 17:47
    
@Mike correct, but even with issues it's much better than endpoint that has no clue about WP at all. This can (and should) be improved some more but it's not urgent task for me at moment. –  Rarst Feb 10 '12 at 19:32
    
Are there any methods of detecting the location of the wp-load.php from within WordPress? For example, could I write a static file with the path set as a variable within it on plugin load, then include that file in the stand alone Ajax response file? –  hereswhatidid Dec 20 '12 at 18:26

I found this and it sped up my ajax.

function my_deregister_heartbeat() {
    global $pagenow;

    if ( 'post.php' != $pagenow && 'post-new.php' != $pagenow ) {
         wp_deregister_script('heartbeat');
         wp_register_script('heartbeat', false);
     }
}
add_action( 'admin_enqueue_scripts', 'my_deregister_heartbeat' );
share|improve this answer

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.