Layout Ajax actions
Last updated
async function makeAjax() {
let response = await fetch('/wp-admin/admin-ajax.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
'action': 'advanced_views',
// todo put your Layout ID
'_layout-id': '6630e2da1953e', // you can learn ID from the shortcode
// todo your args here
'myArg': 'myvalue',
}).toString(),
});
let responseText = await response.text();
let json = {};
try {
json = JSON.parse(responseText);
} catch (e) {
console.log("Error parsing JSON", responseText);
return;
}
console.log('ajax complete', json);
}<?php
declare( strict_types=1 );
use Org\Wplake\Advanced_Views\Bridge\Controllers\Layout\Layout_Controller_Base;
return new class extends Layout_Controller_Base {
public function get_ajax_response(): array {
$myArg = sanitize_text_field( $_POST['myArg'] ?? '' );
// todo your logic here
// additionally, you can use $this->get_container()
// to get PHP-DI instance and access to any of your theme classes
return [
'my_response' => 'Thank you for the feedback!',
];
}
};