Pagination Rel Link Planner
Builds previous, next, and canonical pagination links without query-string leakage.
Purpose
Builds previous, next, and canonical pagination links without query-string leakage.
Snippet details
ContextSeoLevelAdvancedCopy-and-paste statusMarked safe after review.Categories
- Security
Code
<?php
/*
* Copyright (c) 2026 Jeffery L. Paris <jparis@phpog.com>.
* Free for personal and internal use. Paid project use requires visible credit
* to Jeffery L. Paris. Corporate use requires a paid license fee unless a
* separate written license states otherwise.
*/
/**
* Pagination Rel Link Planner.
*
* Purpose:
* Builds previous, next, and canonical pagination links without query-string leakage.
*
* @param string $base_url Clean SEF base URL.
* @param int $current_page Current page number.
* @param int $total_pages Total page count.
* @return array Pagination rel links.
*/
function ogSnippetPaginationRelLinkPlanner(string $base_url, int $current_page, int $total_pages): array {
if ($current_page < 1) {
$current_page = 1;
}
if ($total_pages < 1) {
$total_pages = 1;
}
$base_url = rtrim($base_url, '/');
$links = array('canonical' => $base_url);
if ($current_page > 1) {
$previous_page = $current_page - 1;
if ($previous_page === 1) {
$links['prev'] = $base_url;
} else {
$links['prev'] = $base_url.'/page/'.$previous_page;
}
}
if ($current_page < $total_pages) {
$next_page = $current_page + 1;
$links['next'] = $base_url.'/page/'.$next_page;
}
if ($current_page > 1) {
$links['canonical'] = $base_url.'/page/'.$current_page;
}
return $links;
}
$pagination_links = ogSnippetPaginationRelLinkPlanner('//example.com/colonial-fleet', 3, 9);
echo $pagination_links['prev'];