wppaste
WordPress

_walk_bookmarks( array $bookmarks, string|array $args = '' ): string

Since
2.1.0
Source
wp-includes/bookmark-template.php:51
The formatted output of a list of bookmarks.

Description

The $bookmarks array must contain bookmark objects and will be iterated over to retrieve the bookmark to be used in the output.

The output is formatted as HTML with no way to change that format. However, what is between, before, and after can be changed. The link itself will be HTML.

This function is used internally by wp_list_bookmarks() and should not be used by themes.

Compatibility

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

$bookmarksarray
List of bookmarks to traverse.
$argsstring|arrayoptional
Bookmarks arguments.Default: ''
  • $show_updatedint|booldefault: 0|false

    Whether to show the time the bookmark was last updated. Accepts 1|true or 0|false.
  • $show_descriptionint|booldefault: 0|false

    Whether to show the bookmark description. Accepts 1|true, Accepts 1|true or 0|false.
  • $show_imagesint|booldefault: 1|true

    Whether to show the link image if available. Accepts 1|true or 0|false.
  • $show_nameint|booldefault: 0|false

    Whether to show link name if available. Accepts 1|true or 0|false.
  • $beforestringdefault: <li>

    The HTML or text to prepend to each bookmark.
  • $afterstringdefault: </li>

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

    The HTML or text to prepend to each bookmark inside the anchor tags.
  • $link_afterstringdefault: empty

    The HTML or text to append to each bookmark inside the anchor tags.
  • $betweenstringdefault: "\n"

    The string for use in between the link, description, and image.
  • $show_ratingint|booldefault: 0|false

    Whether to show the link rating. Accepts 1|true or 0|false.

Return value

string
Formatted output in HTML

Performance profile

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

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

Scaling
Scales with input

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

Instructions
13–14

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

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

What it touches

  • optionoption read or writeget_option()called directly
  • hookthird-party callbacksapply_filters()one call below _walk_bookmarks()
  • cacheobject cachewp_cache_get()one call below _walk_bookmarks()
  • serializeserialisationmaybe_unserialize()one call below _walk_bookmarks()

Further down the call graph this can also reach 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 · 1 distinct outcome

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

WhenInstructionsCalls it makes
always13–14wp_parse_args()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev20113–1421
8.520113–1421
8.420113–14216 fewer instructions than PHP 8.3
8.320713–1421
8.220713–1421
8.120713–14213 fewer instructions than PHP 7.4
7.421013–1421

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

Used by · 1

Source code

function _walk_bookmarks( $bookmarks, $args = '' ) {	$defaults = array(		'show_updated'     => 0,		'show_description' => 0,		'show_images'      => 1,		'show_name'        => 0,		'before'           => '<li>',		'after'            => '</li>',		'between'          => "\n",		'show_rating'      => 0,		'link_before'      => '',		'link_after'       => '',	); 	$parsed_args = wp_parse_args( $args, $defaults ); 	$output = ''; // Blank string to start with. 	foreach ( (array) $bookmarks as $bookmark ) {		if ( ! isset( $bookmark->recently_updated ) ) {			$bookmark->recently_updated = false;		}		$output .= $parsed_args['before'];		if ( $parsed_args['show_updated'] && $bookmark->recently_updated ) {			$output .= '<em>';		}		$the_link = '#';		if ( ! empty( $bookmark->link_url ) ) {			$the_link = esc_url( $bookmark->link_url );		}		$desc  = esc_attr( sanitize_bookmark_field( 'link_description', $bookmark->link_description, $bookmark->link_id, 'display' ) );		$name  = esc_attr( sanitize_bookmark_field( 'link_name', $bookmark->link_name, $bookmark->link_id, 'display' ) );		$title = $desc; 		if ( $parsed_args['show_updated'] ) {			if ( ! str_starts_with( $bookmark->link_updated_f, '00' ) ) {				$title .= ' (';				$title .= sprintf(					/* translators: %s: Date and time of last update. */					__( 'Last updated: %s' ),					gmdate(						get_option( 'links_updated_date_format' ),						$bookmark->link_updated_f + (int) ( (float) get_option( 'gmt_offset' ) * HOUR_IN_SECONDS )					)				);				$title .= ')';			}		}		$alt = ' alt="' . $name . ( $parsed_args['show_description'] ? ' ' . $title : '' ) . '"'; 		if ( '' !== $title ) {			$title = ' title="' . $title . '"';		}		$rel = $bookmark->link_rel; 		$target = $bookmark->link_target;		if ( '' !== $target ) {			$target = ' target="' . $target . '"';		} 		if ( '' !== $rel ) {			$rel = ' rel="' . esc_attr( $rel ) . '"';		} 		$output .= '<a href="' . $the_link . '"' . $rel . $title . $target . '>'; 		$output .= $parsed_args['link_before']; 		if ( '' !== $bookmark->link_image && $parsed_args['show_images'] ) {			if ( str_starts_with( $bookmark->link_image, 'http' ) ) {				$output .= '<img src="' . $bookmark->link_image . '"' . $alt . $title . ' />';			} else { // If it's a relative path.				$output .= '<img src="' . get_option( 'siteurl' ) . $bookmark->link_image . '"' . $alt . $title . ' />';			}			if ( $parsed_args['show_name'] ) {				$output .= " $name";			}		} else {			$output .= $name;		}

Changelog

Introduced in 2.1.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/bookmark-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.