get_bookmark()WP 2.1.0

Retrieve Bookmark data

No Hooks.

Return

Array|Object|null. Type returned depends on $output value.

Usage

get_bookmark( $bookmark, $output, $filter );
$bookmark(int|stdClass) (required)
-
$output(string)
The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to an stdClass object, an associative array, or a numeric array, respectively.
Default: OBJECT
$filter(string)
How to sanitize bookmark fields.
Default: 'raw'

Examples

0

#1 Basic Example

Let's get the data of bookmark 13 and display its URL:

<?php
$link = get_bookmark( 13, ARRAY_N );

echo $link->link_url;
?>

In this example, the $link variable will contain the following object:

stdClass Object
(
	[link_id] => 13
	[link_url] => http://www.gogetlinks.net/?inv=n98gxr
	[link_name] => gogetlinks.net
	[link_image] => http://wp-kama.com/wp-includes/images/crystal/archive.png
	[link_target] =>
	[link_description] => Buy/sell quality links, forever
	[link_visible] => Y
	[link_owner] => 1
	[link_rating] => 0
	[link_updated] => 0000-00-00 00:00:00
	[link_rel] =>
	[link_notes] =>
	[link_rss] =>
	[link_category] => Array()
)
0

#2 Display Bookmark Name

<?php 
$bookmark = get_bookmark(5);
echo $bookmark->link_name; 
?>

or:

<?php echo get_bookmark(5)->link_name; ?>
0

#3 Display Bookmark as a Link

<?php  
$bookmark = get_bookmark( 5 );
echo '<a href="'. $bookmark->link_url .'">'. $bookmark->link_name .'</a>';
?>

Notes

  • Global. wpdb. $wpdb WordPress database abstraction object.

Changelog

Since 2.1.0 Introduced.

Code of get_bookmark() WP 6.0

function get_bookmark( $bookmark, $output = OBJECT, $filter = 'raw' ) {
	global $wpdb;

	if ( empty( $bookmark ) ) {
		if ( isset( $GLOBALS['link'] ) ) {
			$_bookmark = & $GLOBALS['link'];
		} else {
			$_bookmark = null;
		}
	} elseif ( is_object( $bookmark ) ) {
		wp_cache_add( $bookmark->link_id, $bookmark, 'bookmark' );
		$_bookmark = $bookmark;
	} else {
		if ( isset( $GLOBALS['link'] ) && ( $GLOBALS['link']->link_id == $bookmark ) ) {
			$_bookmark = & $GLOBALS['link'];
		} else {
			$_bookmark = wp_cache_get( $bookmark, 'bookmark' );
			if ( ! $_bookmark ) {
				$_bookmark = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->links WHERE link_id = %d LIMIT 1", $bookmark ) );
				if ( $_bookmark ) {
					$_bookmark->link_category = array_unique( wp_get_object_terms( $_bookmark->link_id, 'link_category', array( 'fields' => 'ids' ) ) );
					wp_cache_add( $_bookmark->link_id, $_bookmark, 'bookmark' );
				}
			}
		}
	}

	if ( ! $_bookmark ) {
		return $_bookmark;
	}

	$_bookmark = sanitize_bookmark( $_bookmark, $filter );

	if ( OBJECT === $output ) {
		return $_bookmark;
	} elseif ( ARRAY_A === $output ) {
		return get_object_vars( $_bookmark );
	} elseif ( ARRAY_N === $output ) {
		return array_values( get_object_vars( $_bookmark ) );
	} else {
		return $_bookmark;
	}
}