wppaste
WordPress

get_pagenum_link( int $pagenum = 1, bool $escape = true ): string

Since
1.5.0
Source
wp-includes/link-template.php:2424
Retrieves the link for a page number.

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

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
57–127

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 152.

Plugin surface
1 hook

Third-party callbacks on 'get_pagenum_link' run inside this call, and their cost is not bounded by anything here.

Called by
4

4 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • optionoption read or writeget_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.

WhenInstructionsCalls it makes
!->using_permalinks() && !$pagenum57–58remove_query_arg(), home_url(), parse_url(), preg_quote(), ->using_permalinks(), get_bloginfo(), trailingslashit(), apply_filters(), sanitize_url()
!->using_permalinks() && !$pagenum57–58remove_query_arg(), home_url(), parse_url(), preg_quote(), ->using_permalinks(), get_bloginfo(), trailingslashit(), apply_filters(), esc_url()
->using_permalinks() && is_admin() && !$pagenum60–61remove_query_arg(), home_url(), parse_url(), preg_quote(), ->using_permalinks(), is_admin(), get_bloginfo(), trailingslashit(), apply_filters(), sanitize_url()
->using_permalinks() && is_admin() && !$pagenum60–61remove_query_arg(), home_url(), parse_url(), preg_quote(), ->using_permalinks(), is_admin(), get_bloginfo(), trailingslashit(), apply_filters(), esc_url()
!->using_permalinks() && $pagenum63–64remove_query_arg(), home_url(), parse_url(), preg_quote(), ->using_permalinks(), get_bloginfo(), trailingslashit(), add_query_arg(), apply_filters(), sanitize_url()
!->using_permalinks() && $pagenum63–64remove_query_arg(), home_url(), parse_url(), preg_quote(), ->using_permalinks(), get_bloginfo(), trailingslashit(), add_query_arg(), apply_filters(), esc_url()
->using_permalinks() && is_admin() && $pagenum66–67remove_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() && $pagenum66–67remove_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–127remove_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–127remove_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

PHPCompiledExecutedBranchesNotes
8.6-dev15257–12711
8.515257–12711
8.415257–1271118 fewer instructions than PHP 8.3
8.317063–14511
8.217063–14511
8.117063–145111 fewer instruction than PHP 7.4
7.417163–14611

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:

  1. apply_filters( get_pagenum_link )filterline 2490 (+66 into the body)

    Filters the page number link for the current request.

Uses · 11

Used by · 4

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 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.