Pagination Meta Builder
Builds canonical, prev, next, title, and description data for paginated lists.
Function signature
ogBuildPaginationMeta(base_url, page = 1, total_pages = 1, options = array())
Categories
- SEO and Routing
Parameters
base_urlCanonical base URL for pagination metadata.pageCurrent one-based page number.total_pagesTotal number of pages in the paginated result set.optionsOptional documented policy controls for the helper.Return value
Public-safe status string returned by the function for controller branching or logging.
- success
- message
- data
Compatibility
Existing function name, slug, path, and call order preserved; advertised metadata corrected to the actual source behavior.
Minimum PHP version: 7.4
Security notes
Use caller-owned allowlists and context-specific escaping; validate file paths, routes, email tokens, cart totals, discount rules, and tax-region rules before production use.
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.
*/
/**
* Builds canonical, prev, next, title, and description data for paginated lists.
*
* Primary use case: List pages with pagination.
* Typical inputs: base route, page number, total pages.
* Typical output: SEO metadata array.
*
* Implementation note: Avoid canonicalizing all pages to page 1 unless policy demands it.
*
* @param string $base_url Canonical base URL without page suffix.
* @param int $page Current page number.
* @param int $total_pages Total page count.
* @param array $options Title and description options.
* @return array Pagination SEO metadata.
*/
function ogBuildPaginationMeta($base_url, $page = 1, $total_pages = 1, $options = array()) {
$result = array(
'success' => false,
'message' => '',
'data' => array()
);
$base_url = rtrim(trim((string)$base_url), '/');
$page = (int)$page;
$total_pages = (int)$total_pages;
if (!is_array($options)) {
$options = array();
}
if (empty($base_url)) {
$result['message'] = 'Base URL is required.';
return $result;
}
if ($page < 1) {
$page = 1;
}
if ($total_pages < 1) {
$total_pages = 1;
}
if ($page > $total_pages) {
$page = $total_pages;
}
$title = 'Page ' . $page;
if (!empty($options['title'])) {
$title = trim((string)$options['title']);
if ($page > 1) {
$title .= ' - Page ' . $page;
}
}
$canonical = $base_url;
if ($page > 1) {
$canonical .= '/page/' . $page;
}
$prev = '';
if ($page > 1) {
$prev = $base_url;
if (($page - 1) > 1) {
$prev .= '/page/' . ($page - 1);
}
}
$next = '';
if ($page < $total_pages) {
$next = $base_url . '/page/' . ($page + 1);
}
$result['success'] = true;
$result['message'] = 'Pagination metadata built.';
$result['data'] = array(
'title' => $title,
'canonical' => $canonical,
'prev' => $prev,
'next' => $next,
'page' => $page,
'total_pages' => $total_pages
);
return $result;
}