AjaxEndpoint trait¶
Barberklingen\BasePlugin\Traits\AjaxEndpoint
Registers a WordPress AJAX handler (wp_ajax_* / wp_ajax_nopriv_*) and provides response helpers.
Usage¶
use Barberklingen\BasePlugin\Traits\AjaxEndpoint;
class MyAjaxHandler {
use AjaxEndpoint;
public function action_name(): string {
return 'my_action'; // registers wp_ajax_my_action
}
public function allow_private(): bool {
return true; // for logged-in users
}
public function allow_public(): bool {
return false; // not for anonymous users
}
public function execute(): void {
$value = $this->get_request()->input('my_param');
if (!$value) {
$this->error('Missing parameter', 422);
}
$this->json(['result' => $value]);
}
}
MyAjaxHandler::get_instance();
Response methods¶
| Method | HTTP | Description |
|---|---|---|
json($data, $code = 200) | 200 | Send arbitrary JSON |
success($message = null) | 200 | {success: true, message: ...} |
error($message, $code = 500) | $code | {success: false, message: ...} |
create_dialogue_response() | — | Returns a SweetAlert2-compatible response array |
Abstract methods¶
| Method | Description |
|---|---|
action_name(): string | The AJAX action name (without the wp_ajax_ prefix) |
execute(): void | Handler logic |
allow_private(): bool | Register for authenticated users |
allow_public(): bool | Register for unauthenticated users |