wppaste
WordPress

render_block_core_post_navigation_link( array $attributes, string $content ): string

Since
5.9.0
Source
wp-includes/blocks/post-navigation-link.php:18
Renders the core/post-navigation-link block on the server.

Compatibility

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

$attributesarray
Block attributes.
$contentstring
Block default content.

Return value

string
Returns the next or previous post link that is adjacent to the current post.

Performance profile

How much work a call to render_block_core_post_navigation_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
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

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

Instructions
6–112

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What one call costs · 4 distinct outcomes

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

WhenInstructionsCalls it makes
always6–12is_singular()
is_singular() && $navigation_type50–93is_singular(), class(), _x(), sprintf()
is_singular() && $navigation_type && isset($attributes) && $attributes63–101is_singular(), class(), _x(), wp_kses_post(), sprintf()
is_singular() && $navigation_type && isset($attributes) && $attributes76–112is_singular(), class(), _x(), _x(), wp_kses_post(), sprintf(), sprintf()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1476–110204 fewer instructions than PHP 8.5
8.51516–11220
8.41516–11220
8.31516–11220
8.21516–11220
8.11516–11220
7.41516–11220

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.

Uses · 4

  • is_singular()Determines whether the query is for an existing single post of any post type (post, attachment, page, custom post types).
  • get_block_wrapper_attributes()Generates a string of attributes by applying to the current block being rendered all of the features that the block supports.
  • _x()Retrieves translated string with gettext context.
  • wp_kses_post()Sanitizes content for allowed HTML tags for post content.

Source code

function render_block_core_post_navigation_link( $attributes, $content ) {	if ( ! is_singular() ) {		return '';	} 	// Get the navigation type to show the proper link. Available options are `next|previous`.	$navigation_type = $attributes['type'] ?? 'next';	// Allow only `next` and `previous` in `$navigation_type`.	if ( ! in_array( $navigation_type, array( 'next', 'previous' ), true ) ) {		return '';	}	$classes = "post-navigation-link-$navigation_type";	if ( isset( $attributes['textAlign'] ) ) {		$classes .= " has-text-align-{$attributes['textAlign']}";	}	$wrapper_attributes = get_block_wrapper_attributes(		array(			'class' => $classes,		)	);	// Set default values.	$format = '%link';	$link   = 'next' === $navigation_type ? _x( 'Next', 'label for next post link' ) : _x( 'Previous', 'label for previous post link' );	$label  = ''; 	// Only use hardcoded values here, otherwise we need to add escaping where these values are used.	$arrow_map = array(		'none'    => '',		'arrow'   => array(			'next'     => '→',			'previous' => '←',		),		'chevron' => array(			'next'     => '»',			'previous' => '«',		),	); 	// If a custom label is provided, make this a link.	// `$label` is used to prepend the provided label, if we want to show the page title as well.	if ( isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ) {		$label = "{$attributes['label']}";		$link  = $label;	} 	// If we want to also show the page title, make the page title a link and prepend the label.	if ( isset( $attributes['showTitle'] ) && $attributes['showTitle'] ) {		/*		 * If the label link option is not enabled but there is a custom label,		 * display the custom label as text before the linked title.		 */		if ( ! $attributes['linkLabel'] ) {			if ( $label ) {				$format = '<span class="post-navigation-link__label">' . wp_kses_post( $label ) . '</span> %link';			}			$link = '%title';		} elseif ( isset( $attributes['linkLabel'] ) && $attributes['linkLabel'] ) {			// If the label link option is enabled and there is a custom label, display it before the title.			if ( $label ) {				$link = '<span class="post-navigation-link__label">' . wp_kses_post( $label ) . '</span> <span class="post-navigation-link__title">%title</span>';			} else {				/*				 * If the label link option is enabled and there is no custom label,				 * add a colon between the label and the post title.				 */				$label = 'next' === $navigation_type ? _x( 'Next:', 'label before the title of the next post' ) : _x( 'Previous:', 'label before the title of the previous post' );				$link  = sprintf(					'<span class="post-navigation-link__label">%1$s</span> <span class="post-navigation-link__title">%2$s</span>',					wp_kses_post( $label ),					'%title'				);			}		}	} 	// Display arrows.	if ( isset( $attributes['arrow'] ) && 'none' !== $attributes['arrow'] && isset( $arrow_map[ $attributes['arrow'] ] ) ) {		$arrow = $arrow_map[ $attributes['arrow'] ][ $navigation_type ]; 		if ( 'next' === $navigation_type ) {

Changelog

Introduced in 5.9.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 7.1.0 tag, from src/wp-includes/blocks/post-navigation-link.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.