wppaste
WordPress

wp_link_pages( string|array $args = '' ): string

Since
1.2.0, 5.1.0
Source
wp-includes/post-template.php:958
The formatted output of a list of pages.

Description

Displays page links for paginated posts (i.e. including the <!--nextpage--> Quicktag one or more times). This tag must be within The Loop.

Compatibility

WordPress
since 5.1.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

$argsstring|arrayoptional
Array or string of default arguments.Default: ''
  • $beforestringdefault: is &lt;p&gt; Pages:

    HTML or text to prepend to each link.
  • $afterstringdefault: is &lt;/p&gt;

    HTML or text to append to each link.
  • $link_beforestringdefault: empty

    HTML or text to prepend to each link, inside the <a> tag. Also prepended to the current item, which is not linked.
  • $link_afterstringdefault: empty

    HTML or text to append to each Pages link inside the <a> tag. Also appended to the current item, which is not linked.
  • $aria_currentstringdefault: is 'page'

    The value for the aria-current attribute. Possible values are 'page', 'step', 'location', 'date', 'time', 'true', 'false'.
  • $next_or_numberstringdefault: is 'number'

    Indicates whether page numbers should be used. Valid values are number and next.
  • $separatorstringdefault: is ' '

    Text between pagination links.
  • $nextpagelinkstringdefault: is 'Next Page'

    Link text for the next page link, if available.
  • $previouspagelinkstringdefault: is 'Previous Page'

    Link text for the previous page link, if available.
  • $pagelinkstring

    Format string for page numbers. The % in the parameter string will be replaced with the page number, so 'Page %' generates "Page 1", "Page 2", etc. Defaults to '%', just the page number.
  • $echoint|booldefault: 1|true

    Whether to echo or not. Accepts 1|true or 0|false.

Return value

string
Formatted output in HTML.

Performance profile

How much work a call to wp_link_pages() 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
Heavy

Reaches the database via get_post().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
48–99

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

Plugin surface
3 hooks

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

Called by
2

2 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
  • querycontent queryget_post()one call below wp_link_pages()
  • optionoption read or writeget_option()one call below wp_link_pages()

Further down the call graph this can also reach cache, serialize 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 · 3 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_link_pages() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always48–63__(), __(), __(), wp_parse_args(), apply_filters(), apply_filters()
always78–83__(), __(), __(), wp_parse_args(), apply_filters(), _wp_link_page(), apply_filters(), apply_filters()
$prev && $next96–99__(), __(), __(), wp_parse_args(), apply_filters(), _wp_link_page(), apply_filters(), _wp_link_page(), apply_filters(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev15448–9913
8.515448–9913
8.415448–99133 fewer instructions than PHP 8.3
8.315748–9913
8.215748–9913
8.115748–99131 fewer instruction than PHP 7.4
7.415848–9913

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 · 5

5 hooks fire while wp_link_pages() runs, in this order:

  1. apply_filters( wp_link_pages_args )filterline 985 (+27 into the body)

    Filters the arguments used in retrieving page links for paginated posts.

  2. apply_filters( wp_link_pages_link )filterline 1008 (+50 into the body)

    Filters the HTML output of individual page number links.

  3. apply_filters( wp_link_pages_link )filterline 1022 (+64 into the body)

    Filters the HTML output of individual page number links.

  4. apply_filters( wp_link_pages_link )filterline 1032 (+74 into the body)

    Filters the HTML output of individual page number links.

  5. apply_filters( wp_link_pages )filterline 1047 (+89 into the body)

    Filters the HTML output of page links for paginated posts.

Uses · 5

Used by · 2

Source code

function wp_link_pages( $args = '' ) {	global $page, $numpages, $multipage, $more; 	$defaults = array(		'before'           => '<p class="post-nav-links">' . __( 'Pages:' ),		'after'            => '</p>',		'link_before'      => '',		'link_after'       => '',		'aria_current'     => 'page',		'next_or_number'   => 'number',		'separator'        => ' ',		'nextpagelink'     => __( 'Next page' ),		'previouspagelink' => __( 'Previous page' ),		'pagelink'         => '%',		'echo'             => 1,	); 	$parsed_args = wp_parse_args( $args, $defaults ); 	/**	 * Filters the arguments used in retrieving page links for paginated posts.	 *	 * @since 3.0.0	 *	 * @param array $parsed_args An array of page link arguments. See wp_link_pages()	 *                           for information on accepted arguments.	 */	$parsed_args = apply_filters( 'wp_link_pages_args', $parsed_args ); 	$output = '';	if ( $multipage ) {		if ( 'number' === $parsed_args['next_or_number'] ) {			$output .= $parsed_args['before'];			for ( $i = 1; $i <= $numpages; $i++ ) {				$link = $parsed_args['link_before'] . str_replace( '%', $i, $parsed_args['pagelink'] ) . $parsed_args['link_after']; 				if ( $i !== $page || ! $more && 1 === $page ) {					$link = _wp_link_page( $i ) . $link . '</a>';				} elseif ( $i === $page ) {					$link = '<span class="post-page-numbers current" aria-current="' . esc_attr( $parsed_args['aria_current'] ) . '">' . $link . '</span>';				} 				/**				 * Filters the HTML output of individual page number links.				 *				 * @since 3.6.0				 *				 * @param string $link The page number HTML output.				 * @param int    $i    Page number for paginated posts' page links.				 */				$link = apply_filters( 'wp_link_pages_link', $link, $i ); 				// Use the custom links separator beginning with the second link.				$output .= ( 1 === $i ) ? ' ' : $parsed_args['separator'];				$output .= $link;			}			$output .= $parsed_args['after'];		} elseif ( $more ) {			$output .= $parsed_args['before'];			$prev    = $page - 1;			if ( $prev > 0 ) {				$link = _wp_link_page( $prev ) . $parsed_args['link_before'] . $parsed_args['previouspagelink'] . $parsed_args['link_after'] . '</a>'; 				/** This filter is documented in wp-includes/post-template.php */				$output .= apply_filters( 'wp_link_pages_link', $link, $prev );			}			$next = $page + 1;			if ( $next <= $numpages ) {				if ( $prev ) {					$output .= $parsed_args['separator'];				}				$link = _wp_link_page( $next ) . $parsed_args['link_before'] . $parsed_args['nextpagelink'] . $parsed_args['link_after'] . '</a>'; 				/** This filter is documented in wp-includes/post-template.php */				$output .= apply_filters( 'wp_link_pages_link', $link, $next );			}			$output .= $parsed_args['after'];		}	}

Changelog

Introduced in 1.2.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.

5.1.0
Added the aria_current argument.from the docblock
1.2.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/post-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.