Example 1 — Get the link name
Print the name of one bookmark.
<?php
echo esc_html( get_bookmark_field( 'link_name', 2 ) );
?> How It Works
link_nameis the field you want.2is the link ID.esc_html()keeps the output safe.

In WordPress, get_bookmark_field() returns one field of a bookmark. For example, you can get the link name, the URL, or the description.
You give it the field name and the link ID. It returns the value as a string. It does not print it.
Pass the field name first, then the link ID.
get_bookmark_field( 'link_name', 2 );
get_bookmark_field( 'link_url', 2 );
get_bookmark_field( 'link_description', 2, 'display' ); | Call | What it does |
|---|---|
get_bookmark_field( 'link_name', 2 ) | Returns the name of link 2. |
get_bookmark_field( 'link_url', 2 ) | Returns the web address of link 2. |
get_bookmark_field( 'link_description', 2, 'display' ) | Returns the description prepared for display. |
It returns an empty string when the link or the field is missing.
get_bookmark_field( string $field, int $bookmark, string $context = 'display' ): string|WP_Error | Parameter | Description |
|---|---|
$field | Required. The field name, such as link_name, link_url, or link_description. |
$bookmark | Required. The link ID. |
$context | Optional. How to prepare the value. Default is 'display'. Use 'raw' for the stored value. |
Common fields: link_id, link_url, link_name, link_description, link_visible, link_rating, link_rel, link_target, link_image, link_notes, link_owner, link_updated, link_category.
| Return Value | Description |
|---|---|
| string | The value of the field. |
| empty string | The link or the field was not found. |
| WP_Error | An error object. Check with is_wp_error(). |
Three short theme examples. Each one has a sample output.
Print the name of one bookmark.
<?php
echo esc_html( get_bookmark_field( 'link_name', 2 ) );
?> link_name is the field you want.2 is the link ID.esc_html() keeps the output safe.Get the URL and use it in an HTML link.
<?php
$url = get_bookmark_field( 'link_url', 2 );
if ( $url ) {
echo '<a href="' . esc_url( $url ) . '">Visit site</a>';
}
?> $url.if check skips empty values.esc_url() cleans the web address.Handle a missing field or an error before you print.
<?php
$desc = get_bookmark_field( 'link_description', 2 );
if ( is_wp_error( $desc ) || '' === $desc ) {
echo 'No description.';
} else {
echo esc_html( $desc );
}
?> is_wp_error() checks for an error object.Get a whole list of bookmarks and loop over them.
7 people found this page helpful