Wordpress Ajax Techniques

I recently played with the introduction of AJAX in WordPress. I know that many plugins are available, but I wanted to do it myself.

In AJAXified WordPress related articles, most people recommend using admin-ajax.php to process AJAX requests. My first idea on how to do this is to simply create custom get_header () and get_footer ()

1st method

// 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 the page templates will look like

<?php ajax_get_header(); ?>

<!-- Content -->

<?php ajax_get_footer(); ?>

And creating ajax calls the standard way, of course. This method seems simple and clean to me. On the other hand, many people recommend using the built-in function, creating a hook for calling AJAX calls.

Second 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 to admin-ajax.php

Which one to use?

, , . (ajax_get_header) 400 ( ) 2- (admin-ajax.php) 800 . , , ​​WP - .

, , AJAX- admin-ajax.php? ? ?

+5
1

, Wordpress ajax, admin-ajax.php , core hook hooks , , , ajax call .

, , . .

. - , wordpress, .

. 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',
);
+1

All Articles