get_pagenum_link( int $pagenum = 1, bool $escape = true ): string
- Since
- 1.5.0
- Source
wp-includes/link-template.php:2424
Compatibility
- WordPress
- since 1.5.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$pagenumintoptional- Page number. Default 1.Default:
1 $escapebooloptional- Whether to escape the URL for display, with esc_url().
If set to false, prepares the URL with sanitize_url(). Default true.Default:true
Return value
string- The link URL for the given page number.
Performance profile
How much work a call to get_pagenum_link() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Moderate
- Scaling
- Constant
- Instructions
- 57–127
- Plugin surface
- 1 hook
- Called by
- 4
Reads stored settings via get_option(), cached per request but not free on a cold cache.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 152.
Third-party callbacks on 'get_pagenum_link' run inside this call, and their cost is not bounded by anything here.
4 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - optionoption read or write
get_option()one call below get_pagenum_link()
Further down the call graph this can also reach cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 10 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_pagenum_link() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!->using_permalinks() && !$pagenum | 57–58 | remove_query_arg(), home_url(), parse_url(), preg_quote(), ->using_permalinks(), get_bloginfo(), trailingslashit(), apply_filters(), sanitize_url() |
!->using_permalinks() && !$pagenum | 57–58 | remove_query_arg(), home_url(), parse_url(), preg_quote(), ->using_permalinks(), get_bloginfo(), trailingslashit(), apply_filters(), esc_url() |
->using_permalinks() && is_admin() && !$pagenum | 60–61 | remove_query_arg(), home_url(), parse_url(), preg_quote(), ->using_permalinks(), is_admin(), get_bloginfo(), trailingslashit(), apply_filters(), sanitize_url() |
->using_permalinks() && is_admin() && !$pagenum | 60–61 | remove_query_arg(), home_url(), parse_url(), preg_quote(), ->using_permalinks(), is_admin(), get_bloginfo(), trailingslashit(), apply_filters(), esc_url() |
!->using_permalinks() && $pagenum | 63–64 | remove_query_arg(), home_url(), parse_url(), preg_quote(), ->using_permalinks(), get_bloginfo(), trailingslashit(), add_query_arg(), apply_filters(), sanitize_url() |
!->using_permalinks() && $pagenum | 63–64 | remove_query_arg(), home_url(), parse_url(), preg_quote(), ->using_permalinks(), get_bloginfo(), trailingslashit(), add_query_arg(), apply_filters(), esc_url() |
->using_permalinks() && is_admin() && $pagenum | 66–67 | remove_query_arg(), home_url(), parse_url(), preg_quote(), ->using_permalinks(), is_admin(), get_bloginfo(), trailingslashit(), add_query_arg(), apply_filters(), sanitize_url() |
->using_permalinks() && is_admin() && $pagenum | 66–67 | remove_query_arg(), home_url(), parse_url(), preg_quote(), ->using_permalinks(), is_admin(), get_bloginfo(), trailingslashit(), add_query_arg(), apply_filters(), esc_url() |
->using_permalinks() && !is_admin() | 109–127 | remove_query_arg(), home_url(), parse_url(), preg_quote(), ->using_permalinks(), is_admin(), preg_match(), get_bloginfo(), untrailingslashit(), preg_quote(), ltrim(), ->using_index_permalinks(), untrailingslashit(), array_filter(), user_trailingslashit(), apply_filters(), sanitize_url() |
->using_permalinks() && !is_admin() | 109–127 | remove_query_arg(), home_url(), parse_url(), preg_quote(), ->using_permalinks(), is_admin(), preg_match(), get_bloginfo(), untrailingslashit(), preg_quote(), ltrim(), ->using_index_permalinks(), untrailingslashit(), array_filter(), user_trailingslashit(), apply_filters(), esc_url() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 152 | 57–127 | 11 | |
| 8.5 | 152 | 57–127 | 11 | |
| 8.4 | 152 | 57–127 | 11 | 18 fewer instructions than PHP 8.3 |
| 8.3 | 170 | 63–145 | 11 | |
| 8.2 | 170 | 63–145 | 11 | |
| 8.1 | 170 | 63–145 | 11 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 171 | 63–146 | 11 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while get_pagenum_link() runs, in this order:
- apply_filters( get_pagenum_link )filterline 2490 (+66 into the body)
Filters the page number link for the current request.
Uses · 11
- remove_query_arg()Removes an item or items from a query string.
- home_url()Retrieves the URL for the current site where the front end is accessible.
- is_admin()Determines whether the current request is for an administrative interface page.
- trailingslashit()Appends a trailing slash.
- get_bloginfo()Retrieves information about the current site.
- add_query_arg()Retrieves a modified URL query string.
- untrailingslashit()Removes trailing forward slashes and backslashes if they exist.
- user_trailingslashit()Retrieves a trailing-slashed string if the site is set for adding trailing slashes.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- esc_url()Checks and cleans a URL.
- sanitize_url()Sanitizes a URL for database or redirect usage.
Used by · 4
- get_next_posts_page_link()Retrieves the next posts page link.
- get_previous_posts_page_link()Retrieves the previous posts page link.
- paginate_links()Retrieves paginated links for archive post pages.
- twentyfourteen_paging_nav()Display navigation to next/previous set of posts when applicable.
Source code
function get_pagenum_link( $pagenum = 1, $escape = true ) { global $wp_rewrite; $pagenum = (int) $pagenum; $request = remove_query_arg( 'paged' ); $home_root = parse_url( home_url() ); $home_root = ( isset( $home_root['path'] ) ) ? $home_root['path'] : ''; $home_root = preg_quote( $home_root, '|' ); $request = preg_replace( '|^' . $home_root . '|i', '', $request ); $request = preg_replace( '|^/+|', '', $request ); if ( ! $wp_rewrite->using_permalinks() || is_admin() ) { $base = trailingslashit( get_bloginfo( 'url' ) ); if ( $pagenum > 1 ) { $result = add_query_arg( 'paged', $pagenum, $base . $request ); } else { $result = $base . $request; } } else { $qs_regex = '|\?.*?$|'; preg_match( $qs_regex, $request, $qs_match ); $parts = array(); $parts[] = untrailingslashit( get_bloginfo( 'url' ) ); if ( ! empty( $qs_match[0] ) ) { $query_string = $qs_match[0]; $request = preg_replace( $qs_regex, '', $request ); } else { $query_string = ''; } $request = preg_replace( "|$wp_rewrite->pagination_base/\d+/?$|", '', $request ); $request = preg_replace( '|^' . preg_quote( $wp_rewrite->index, '|' ) . '|i', '', $request ); $request = ltrim( $request, '/' ); if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' !== $request ) ) { $parts[] = $wp_rewrite->index; } $parts[] = untrailingslashit( $request ); if ( $pagenum > 1 ) { $parts[] = $wp_rewrite->pagination_base; $parts[] = $pagenum; } $result = user_trailingslashit( implode( '/', array_filter( $parts ) ), 'paged' ); if ( ! empty( $query_string ) ) { $result .= $query_string; } } /** * Filters the page number link for the current request. * * @since 2.5.0 * @since 5.2.0 Added the `$pagenum` argument. * * @param string $result The page number link. * @param int $pagenum The page number. */ $result = apply_filters( 'get_pagenum_link', $result, $pagenum ); if ( $escape ) { return esc_url( $result ); } else { return sanitize_url( $result ); }}Changelog
Introduced in 1.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 tag, from
src/wp-includes/link-template.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.