WordPress REST API vs Admin AJAX

Summary: Comparing WordPress REST API and Admin AJAX perfomance.

When deciding between using WordPress’s AJAX (admin-ajax.php) and the REST API for asynchronous requests, several factors come into play, including performance, flexibility, and compatibility.

Introduction to AJAX in WordPress

Before the WordPress REST API was introduced in WordPress 4.7 developers primarily used AJAX (Asynchronous JavaScript and XML) to handle asynchronous requests in WordPress. The admin-ajax.php file in the /wp-admin directory processes these requests.

How AJAX Requests Work

When an AJAX request is made to admin-ajax.php WordPress loads several core files:

  • /wp-load.php
  • /wp-config.php
  • /wp-settings.php (loads core files, all active plugins, themes, and the REST API)
  • /wp-admin/includes/admin.php
  • /wp-admin/includes/ajax-actions.php

After these files are loaded, WordPress hooks into the admin_init action, which triggers several core functions. After all these functions are executed, WordPress finally calls the AJAX action defined by $_GET['action'] or $_POST['action'].

Example AJAX Handler

// In your plugin or theme's functions.php

function my_ajax_handler() {
    $response = array(
        'time' => time(),
        'message' => 'AJAX request successful!',
    );
    wp_send_json($response);
}

// Hook the function to a specific AJAX action
add_action('wp_ajax_my_ajax_action', 'my_ajax_handler');
add_action('wp_ajax_nopriv_my_ajax_action', 'my_ajax_handler');
  • wp_ajax_my_ajax_action is used for logged-in users.
  • wp_ajax_nopriv_my_ajax_action is used for non-logged-in users.

Performance Considerations

Since admin-ajax.php loads many core files and plugins, it can be relatively slow, especially on sites with many plugins. This extra overhead can result in higher response times.

WordPress REST API

The REST API is a more modern alternative to AJAX for handling asynchronous requests. Unlike AJAX, it does not load the WordPress admin or fire the admin_init hook, which can reduce overhead.

How REST API Requests Work

A typical REST API request passes through the following files:

  • /index.php
  • /wp-blog-header.php
  • /wp-load.php
  • /wp-config.php
  • /wp-settings.php (loads core files, plugins, themes, and the REST API)

REST API endpoints are registered using the rest_api_init hook.

Example REST API Endpoint

// In your plugin or theme's functions.php

function my_custom_rest_route() {
    register_rest_route('myplugin/v1', '/time', array(
        'methods' => 'GET',
        'callback' => 'my_rest_api_handler',
        'permission_callback' => '__return_true',
    ));
}

function my_rest_api_handler() {
    return new WP_REST_Response(array(
        'time' => time(),
        'message' => 'REST API request successful!',
    ), 200);
}

add_action('rest_api_init', 'my_custom_rest_route');

GET request to /wp-json/myplugin/v1/time will trigger the my_rest_api_handler function, returning a JSON response with the current time.

Performance Considerations

The REST API generally has less overhead compared to admin-ajax.php because it skips loading the admin interface and admin_init hook. This results in slightly faster response times.

Custom Endpoints via Must-Use Plugins

For maximum performance we can create custom endpoints in a must-use (MU) plugin. MU plugins are loaded before any other plugins and themes allowing us to handle requests with minimal overhead.

Example Must-Use Plugin

// Save this file as /wp-content/mu-plugins/custom-endpoint.php

/**
 * Plugin Name: Custom Endpoint
 */

if (isset($_GET['custom-ajax'])) {
    // Define the DOING_AJAX constant to mimic an AJAX environment
    if (!defined('DOING_AJAX')) {
        define('DOING_AJAX', true);
    }

    wp_send_json(array(
        'time' => time(),
        'message' => 'Custom request successful!',
    ));
    exit;
}

Accessing https://yourdomain.com/?custom-ajax=1 will trigger this endpoint, returning a JSON response with minimal WordPress overhead.

Performance Considerations

This approach is significantly faster than both admin-ajax.php and the REST API.

  • It bypasses the loading of most WordPress core files, themes, and plugins.
  • Only the essential WordPress environment is loaded.

Conclusion

  • AJAX (admin-ajax.php): Best for legacy support or when you need admin functionality.
  • WordPress REST API: Ideal for modern development, better performance than AJAX, and supports RESTful architecture.
  • Custom MU Plugin Endpoint: Best for high-performance tasks with minimal overhead.
«
»

Leave a Reply

Your email address will not be published. Required fields are marked *