Plugin Support
qtwrk
(@qtwrk)
no , the issue is about admin-ajax.php itself , but more like the nonce token
on which page you got it 403 ?
on the main page of the atravisio.com website there is a pagination logic, which loads more contents upon scroll.
once the cache is stored though, if you start scrolling, you end up with a 403 error on admin-ajax.php page and the paginations fails.
right now I have purged the cache and therefore you won’t face the error, once the cache will be stored, the error will rise again.
It’s not a nonce issue, as the code worked fine before installing LSC and everytime I purge the cache…
<?php namespace App; use App\View\Composers\Homepage; use function Roots\view; class Ajax { /** * Action argument used by the nonce validating the AJAX request. * * @var string */ public const NONCE = 'ajax-nonce'; /** * Ajax constructor. */ public static function register(): void { $handler = new self(); add_action('wp_ajax_load_more_articles', [$handler, 'loadMore']); add_action('wp_ajax_nopriv_load_more_articles', [$handler, 'loadMore']); } public static function getNonce(): string { return wp_create_nonce(self::NONCE); } private static function checkAjaxReferer(): void { check_ajax_referer(self::NONCE, 'security'); } public function loadMore(): void { self::checkAjaxReferer(); $page = isset($_POST['page']) ? intval($_POST['page']) : 1; $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : null; $catID = isset($_POST['catID']) && $_POST['catID'] !== '' ? intval($_POST['catID']) : null; $composer = new Homepage(); $per_page = get_option('paginazione'); $data = $composer->griglia($page, $per_page, $post_id, $catID); if (!empty($data['items'])) { $html = view('components.griglia-hp', ['griglia' => $data])->render(); wp_send_json_success(['html' => $html]); } else { wp_send_json_error('No more items'); } } } Ajax::register();
and the JS:
... $.ajax({ type: 'POST', url: wp['ajaxUrl'], data: { action: 'load_more_articles', security: wp['security'], page: currentPage + 1, post_id: postID, catID: catID, }, headers: { 'X-Requested-With': 'XMLHttpRequest' // Intestazione aggiuntiva per le richieste AJAX }, ....
Plugin Support
qtwrk
(@qtwrk)
it is nonce issue , nonce has 12 – 24 hours validity time , so it works for while after you purge cache , then breaks again as nonce expired
go to LiteSpeed Cache -> ESI -> enable ESI, add
ajax-nonce
into ESI nonce list, save and purge all
so something like that? but this should work not just for logged in users… as the pagination appears in home page for everyone…
Plugin Support
qtwrk
(@qtwrk)
yes, just like that
login user has shorter TTL, it won’t encounter issue like nonce expiration