The get_pagenum_link WordPress PHP filter allows you to modify the page number link for the current request.
Usage
add_filter('get_pagenum_link', 'your_custom_function', 10, 2);
function your_custom_function($result, $pagenum) {
// your custom code here
return $result;
}
Parameters
- $result (string): The page number link that you want to modify.
- $pagenum (int): The page number you’re working with.
More information
See WordPress Developer Resources: get_pagenum_link
Examples
Add UTM Parameters
Add UTM parameters to the page number links for tracking purposes.
add_filter('get_pagenum_link', 'add_utm_parameters', 10, 2);
function add_utm_parameters($result, $pagenum) {
$result = add_query_arg(array(
'utm_source' => 'your_source',
'utm_medium' => 'your_medium',
'utm_campaign' => 'your_campaign'
), $result);
return $result;
}
Append Custom Query
Append a custom query parameter to the page number links.
add_filter('get_pagenum_link', 'append_custom_query', 10, 2);
function append_custom_query($result, $pagenum) {
$result = add_query_arg('custom_param', 'custom_value', $result);
return $result;
}
Remove Query Parameters
Remove specific query parameters from the page number links.
add_filter('get_pagenum_link', 'remove_query_parameters', 10, 2);
function remove_query_parameters($result, $pagenum) {
$result = remove_query_arg(array('parameter1', 'parameter2'), $result);
return $result;
}
Add Hash Fragment
Add a hash fragment to the page number links.
add_filter('get_pagenum_link', 'add_hash_fragment', 10, 2);
function add_hash_fragment($result, $pagenum) {
$result = $result . '#your-hash-fragment';
return $result;
}
Change Domain
Change the domain of the page number links.
add_filter('get_pagenum_link', 'change_domain', 10, 2);
function change_domain($result, $pagenum) {
$result = str_replace('http://example.com', 'https://new-domain.com', $result);
return $result;
}