Skip to content

AJAX

The Ajax facade simplifies the management of WordPress AJAX calls by providing a fluent, chainable API.

Use the listen() method to register an AJAX handler:

use Pollora\Support\Facades\Ajax;
Ajax::listen('my_action', function () {
wp_send_json_success(['message' => 'It works!']);
});

This automatically registers WordPress hooks on wp_ajax_my_action and wp_ajax_nopriv_my_action.

By default, AJAX handlers are available to all users. You can restrict access:

// Only logged-in users
Ajax::listen('my_action', function () {
wp_send_json_success(['user' => wp_get_current_user()->display_name]);
})->forLoggedUsers();
// Only guest users (not logged in)
Ajax::listen('my_action', function () {
wp_send_json_success(['message' => 'Hello guest!']);
})->forGuestUsers();

You can reference a controller instead of a closure:

Ajax::listen('load_more_posts', [PostController::class, 'loadMore'])
->forLoggedUsers();

Send AJAX requests from JavaScript using the ajaxurl global provided by WordPress:

jQuery.post(ajaxurl, {
action: 'my_action',
_wpnonce: myApp.nonce,
data: 'some data'
}, function (response) {
if (response.success) {
console.log(response.data);
}
});

Make sure to localize your script with the nonce:

use Pollora\Support\Facades\Asset;
Asset::add('my-script', 'assets/js/app.js')
->toFrontend()
->localize('myApp', [
'nonce' => wp_create_nonce('wp_rest'),
]);

When you call Ajax::listen(), Pollora registers WordPress action hooks:

MethodWordPress Hook
Default (both)wp_ajax_{action} + wp_ajax_nopriv_{action}
forLoggedUsers()wp_ajax_{action} only
forGuestUsers()wp_ajax_nopriv_{action} only

The callback receives the standard WordPress AJAX context. Use wp_send_json_success() / wp_send_json_error() to send responses.