From 05bb9beb879a077ec30812e8b5d4465dd81b4e03 Mon Sep 17 00:00:00 2001 From: Mario Santos Date: Wed, 19 Oct 2022 19:01:53 +0200 Subject: [PATCH 1/2] Add prefetch and prevent scroll in pagination --- wp-directives.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/wp-directives.php b/wp-directives.php index 5298a26b..3acdc653 100644 --- a/wp-directives.php +++ b/wp-directives.php @@ -47,11 +47,18 @@ function add_wp_link_attribute($block_content) } $link = parse_url($w->get_attribute('href')); + $classes = $w->get_attribute('class'); if (!isset($link['host']) || $link['host'] === $site_url['host']) { - $w->set_attribute('wp-link', 'true'); + if (str_contains($classes, 'query-pagination') || str_contains($classes, 'page-numbers')) { + $w->set_attribute('wp-link', '{ "prefetch": true, "scroll": false }'); + } else { + $w->set_attribute('wp-link', '{ "prefetch": true }'); + } } } return (string) $w; } -add_filter('render_block', 'add_wp_link_attribute', 10, 2); +// We go only through the Query Loops and the template parts until we find a better solution. +add_filter('render_block_core/query', 'add_wp_link_attribute', 10, 1); +add_filter('render_block_core/template-part', 'add_wp_link_attribute', 10, 1); From f50f3854dd965b02a10a4049e093e7ac8b0a7d11 Mon Sep 17 00:00:00 2001 From: Mario Santos Date: Thu, 20 Oct 2022 09:37:30 +0200 Subject: [PATCH 2/2] Move `$classes` inside the `if` statement --- wp-directives.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wp-directives.php b/wp-directives.php index 3acdc653..23b94e07 100644 --- a/wp-directives.php +++ b/wp-directives.php @@ -47,8 +47,8 @@ function add_wp_link_attribute($block_content) } $link = parse_url($w->get_attribute('href')); - $classes = $w->get_attribute('class'); if (!isset($link['host']) || $link['host'] === $site_url['host']) { + $classes = $w->get_attribute('class'); if (str_contains($classes, 'query-pagination') || str_contains($classes, 'page-numbers')) { $w->set_attribute('wp-link', '{ "prefetch": true, "scroll": false }'); } else {