_walk_bookmarks( array $bookmarks, string|array $args = '' ): string
- Since
- 2.1.0
- Source
wp-includes/bookmark-template.php:51
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|falseWhether to show the time the bookmark was last updated. Accepts 1|true or 0|false.$show_descriptionint|booldefault: 0|falseWhether to show the bookmark description. Accepts 1|true, Accepts 1|true or 0|false.$show_imagesint|booldefault: 1|trueWhether to show the link image if available. Accepts 1|true or 0|false.$show_nameint|booldefault: 0|falseWhether 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: emptyThe HTML or text to prepend to each bookmark inside the anchor tags.$link_afterstringdefault: emptyThe 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|falseWhether 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
- Scaling
- Scales with input
- Instructions
- 13–14
- Plugin surface
- None
- Called by
- 1
Reads stored settings via get_option(), cached per request but not free on a cold cache.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 212.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- optionoption read or write
get_option()called directly - hookthird-party callbacks
apply_filters()one call below _walk_bookmarks() - cacheobject cache
wp_cache_get()one call below _walk_bookmarks() - serializeserialisation
maybe_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 13–14 | wp_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 212 | 13–14 | 24 | |
| 8.5 | 212 | 13–14 | 24 | |
| 8.4 | 212 | 13–14 | 24 | 11 fewer instructions than PHP 8.3 |
| 8.3 | 223 | 13–14 | 24 | |
| 8.2 | 223 | 13–14 | 24 | |
| 8.1 | 223 | 13–14 | 24 | 3 fewer instructions than PHP 7.4 |
| 7.4 | 226 | 13–14 | 24 |
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 · 8
- wp_parse_args()Merges user defined arguments into defaults array.
- esc_url()Checks and cleans a URL.
- esc_attr()Escaping for HTML attributes.
- sanitize_bookmark_field()Sanitizes a bookmark field.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- __()Retrieves the translation of $text.
- get_option()Retrieves an option value based on an option name.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
Used by · 1
- wp_list_bookmarks()Retrieves or echoes all of the bookmarks.
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 ) { if ( is_string( $rel ) && '' !== $rel ) { if ( ! str_contains( $rel, 'noopener' ) ) { $rel = trim( $rel ) . ' noopener'; } } else { $rel = 'noopener'; } $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.Changelog
Introduced in 2.1.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.7.7 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.