Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress Ajax Techniques

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.

In articles regarding to AJAXified WordPress most people recommend using admin-ajax.php to process AJAX requests. My first idea how to make it was to simply create custom get_header() and get_footer()

1st way

// 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 */

Then, page templates would look like

<?php ajax_get_header(); ?>

<!-- Content -->

<?php ajax_get_footer(); ?>

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.

2nd way

function process_ajax(){
  /* Show the page or whatever */
}
add_action('wp_ajax_nopriv_ajax', 'process_ajax');
add_action('wp_ajax_ajax', 'process_ajax');

And pointing AJAX calls to admin-ajax.php

Which one to use?

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.

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?

like image 409
Hynek Zatloukal Avatar asked Mar 30 '12 17:03

Hynek Zatloukal


People also ask

How do we use AJAX with WordPress?

Here's what the process for using Ajax in WordPress looks like: The user triggers an Ajax request, which is first passed to the admin-ajax. php file in the wp-admin folder. The Ajax request needs to supply at least one piece of data (using the GET or POST method).

How can I tell if WordPress AJAX is working?

To see if the current request is an AJAX request sent from a js library ( like jQuery ), you could try something like this: if( ! empty( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] ) && strtolower( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ]) == 'xmlhttprequest' ) { //This is an ajax request. }

What does AJAX mean in WordPress?

AJAX stands for Asynchronous JavaScript and XML. AJAX is combination of web scripts and technologies that enables web pages to be updated without reloading the entire page. In WordPress, you can see AJAX in action in the post edit screen, where you can add a new category while writing a post without reloading the page.


1 Answers

Your 1st way is always going to be faster than Wordpress's own ajax since admin-ajax.php takes care of lot of other things like core admin hooks and other function calls which in turn makes the whole ajax call pretty huge.

in the first way, You are not bothering about anything else other than your own functions and output. That increases the performance.

There is no hard and fast rule what to use, The first way you can do things faster but they won't be hookable with wordpress's core admin functions which may be a disadvantage for some purpose.

See admin-ajax.php

$core_actions_get = array(
'fetch-list', 'ajax-tag-search', 'wp-compression-test', 'imgedit-preview', 'oembed-cache',
'autocomplete-user', 'dashboard-widgets', 'logged-in',
);

$core_actions_post = array(
'oembed-cache', 'image-editor', 'delete-comment', 'delete-tag', 'delete-link',
'delete-meta', 'delete-post', 'trash-post', 'untrash-post', 'delete-page', 'dim-comment',
'add-link-category', 'add-tag', 'get-tagcloud', 'get-comments', 'replyto-comment',
'edit-comment', 'add-menu-item', 'add-meta', 'add-user', 'autosave', 'closed-postboxes',
'hidden-columns', 'update-welcome-panel', 'menu-get-metabox', 'wp-link-ajax',
'menu-locations-save', 'menu-quick-search', 'meta-box-order', 'get-permalink',
'sample-permalink', 'inline-save', 'inline-save-tax', 'find_posts', 'widgets-order',
'save-widget', 'set-post-thumbnail', 'date_format', 'time_format', 'wp-fullscreen-save-post',
'wp-remove-post-lock', 'dismiss-wp-pointer', 'upload-attachment',
);
like image 128
Praveen Puglia Avatar answered Oct 10 '22 02:10

Praveen Puglia