Using WordPress ‘clean_bookmark_cache()’ PHP function

The clean_bookmark_cache() WordPress PHP function deletes the bookmark cache for a specific bookmark.

Usage

To use the clean_bookmark_cache() function, you simply need to call it and pass the ID of the bookmark as a parameter. Here’s an example:

clean_bookmark_cache($bookmark_id);

In this example, replace $bookmark_id with the actual ID of the bookmark you want to clear from the cache.

Parameters

  • $bookmark_id (int) – This is the ID of the bookmark for which you want to clear the cache.

More information

See WordPress Developer Resources: clean_bookmark_cache()

This function was implemented in WordPress 2.0.0. It is not deprecated and can be found in the WordPress source code at wp-includes/bookmark.php.

Examples

Clearing a Bookmark Cache

Let’s say you have a bookmark with ID 123. You can clear its cache like so:

$bookmark_id = 123;
clean_bookmark_cache($bookmark_id);

This will clear the cache for the bookmark with ID 123.

Clearing Multiple Bookmark Caches

If you have an array of bookmark IDs and want to clear the cache for all of them, you can do this:

$bookmark_ids = array(123, 456, 789);
foreach($bookmark_ids as $id) {
  clean_bookmark_cache($id);
}

This goes through each ID in the $bookmark_ids array and clears the cache for each bookmark.

Clearing Bookmark Cache After Updating a Bookmark

If you’ve updated a bookmark, you might want to clear its cache to ensure the changes are reflected immediately:

$bookmark_id = 123;
wp_update_link(array('link_id' => $bookmark_id, 'link_url' => 'https://new-url.com'));
clean_bookmark_cache($bookmark_id);

This updates the URL of the bookmark with ID 123 and then clears its cache.

Clearing Bookmark Cache Inside a Function

You could also incorporate this into your own function to clear the cache after performing some task:

function update_bookmark_and_clear_cache($bookmark_id, $new_url) {
  wp_update_link(array('link_id' => $bookmark_id, 'link_url' => $new_url));
  clean_bookmark_cache($bookmark_id);
}

update_bookmark_and_clear_cache(123, 'https://new-url.com');

This creates a new function update_bookmark_and_clear_cache(), which updates a bookmark’s URL and clears its cache. It then calls this function for a bookmark with ID 123.

Conditional Clearing of Bookmark Cache

You might want to conditionally clear the bookmark cache, like so:

$bookmark_id = 123;
$bookmark = get_bookmark($bookmark_id);

if($bookmark->link_url == 'https://old-url.com') {
  wp_update_link(array('link_id' => $bookmark_id, 'link_url' => 'https://new-url.com'));
  clean_bookmark_cache($bookmark_id);
}

This checks if the URL of the bookmark with ID 123 is ‘https://old-url.com‘. If it is, it updates the URL and clears the bookmark’s cache.