wp_insert_link( array $linkdata, bool $wp_error = false ): int|WP_Error
- Since
- 2.0.0
- Source
wp-admin/includes/bookmark.php:174
Description
Runs all the necessary sanitizing, provides default values if arguments are missing, and finally saves the link.
Compatibility
- WordPress
- since 2.0.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
$linkdataarray- Elements that make up the link to insert.
$link_idintOptional. The ID of the existing link if updating.$link_urlstringThe URL the link points to.$link_namestringThe title of the link.$link_imagestringOptional. A URL of an image.$link_targetstringOptional. The target element for the anchor tag.$link_descriptionstringOptional. A short description of the link.$link_visiblestringOptional. 'Y' means visible, anything else means not.$link_ownerintOptional. A user ID.$link_ratingintOptional. A rating for the link.$link_relstringOptional. A relationship of the link to you.$link_notesstringOptional. An extended description of or notes on the link.$link_rssstringOptional. A URL of an associated RSS feed.$link_categoryintOptional. The term ID of the link category. If empty, uses default link category.
$wp_errorbooloptional- Whether to return a WP_Error object on failure. Default false.Default:
false
Return value
int|WP_Error- The link ID on success. The value 0 or WP_Error on failure.
Performance profile
How much work a call to wp_insert_link() 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
- Constant
- Instructions
- 112–140
- Plugin surface
- 2 hooks
- Called by
- 2
Reaches the database via ->insert().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 217.
Third-party callbacks on 'edit_link', 'add_link' run inside this call, and their cost is not bounded by anything here.
2 places 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
do_action()called directly - sqldatabase query
->insert()called directly - cacheobject cache
wp_cache_get()one call below wp_insert_link() - serializeserialisation
maybe_unserialize()one call below wp_insert_link()
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 · 24 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_insert_link() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($link_id) && is_array($link_category) | 112–118 | wp_parse_args(), sanitize_bookmark(), wp_unslash(), current_time(), gmdate(), compact(), ->insert() |
empty($link_id) && empty($parsed_args) && is_array($link_category) | 113–119 | wp_parse_args(), sanitize_bookmark(), wp_unslash(), get_current_user_id(), current_time(), gmdate(), compact(), ->insert() |
empty($link_id) | 114–123 | wp_parse_args(), sanitize_bookmark(), wp_unslash(), current_time(), gmdate(), get_option(), compact(), ->insert() |
empty($link_id) && empty($parsed_args) | 115–124 | wp_parse_args(), sanitize_bookmark(), wp_unslash(), get_current_user_id(), current_time(), gmdate(), get_option(), compact(), ->insert() |
empty($link_id) && is_array($link_category) | 116–122 | wp_parse_args(), sanitize_bookmark(), wp_unslash(), current_time(), gmdate(), compact(), compact(), ->update() |
empty($link_id) && empty($parsed_args) && is_array($link_category) | 117–123 | wp_parse_args(), sanitize_bookmark(), wp_unslash(), get_current_user_id(), current_time(), gmdate(), compact(), compact(), ->update() |
empty($link_id) | 118–127 | wp_parse_args(), sanitize_bookmark(), wp_unslash(), current_time(), gmdate(), get_option(), compact(), compact(), ->update() |
empty($link_id) && empty($parsed_args) | 119–128 | wp_parse_args(), sanitize_bookmark(), wp_unslash(), get_current_user_id(), current_time(), gmdate(), get_option(), compact(), compact(), ->update() |
empty($link_id) && is_array($link_category) | 122–128 | wp_parse_args(), sanitize_bookmark(), wp_unslash(), current_time(), gmdate(), compact(), ->insert(), __() |
empty($link_id) && empty($parsed_args) && is_array($link_category) | 123–129 | wp_parse_args(), sanitize_bookmark(), wp_unslash(), get_current_user_id(), current_time(), gmdate(), compact(), ->insert(), __() |
empty($link_id) | 124–133 | wp_parse_args(), sanitize_bookmark(), wp_unslash(), current_time(), gmdate(), get_option(), compact(), ->insert(), __() |
empty($link_id) && empty($parsed_args) | 125–134 | wp_parse_args(), sanitize_bookmark(), wp_unslash(), get_current_user_id(), current_time(), gmdate(), get_option(), compact(), ->insert(), __() |
12 further outcomes, up to 140 instructions
empty($link_id) && is_array($link_category) | 126–132 | wp_parse_args(), sanitize_bookmark(), wp_unslash(), current_time(), gmdate(), compact(), compact(), ->update(), __() |
empty($link_id) && is_array($link_category) | 126–133 | wp_parse_args(), sanitize_bookmark(), wp_unslash(), current_time(), gmdate(), compact(), ->insert(), wp_set_link_cats(), do_action(), clean_bookmark_cache() |
empty($link_id) && empty($parsed_args) && is_array($link_category) | 127–133 | wp_parse_args(), sanitize_bookmark(), wp_unslash(), get_current_user_id(), current_time(), gmdate(), compact(), compact(), ->update(), __() |
empty($link_id) && empty($parsed_args) && is_array($link_category) | 127–134 | wp_parse_args(), sanitize_bookmark(), wp_unslash(), get_current_user_id(), current_time(), gmdate(), compact(), ->insert(), wp_set_link_cats(), do_action(), clean_bookmark_cache() |
empty($link_id) && is_array($link_category) | 127–134 | wp_parse_args(), sanitize_bookmark(), wp_unslash(), current_time(), gmdate(), compact(), compact(), ->update(), wp_set_link_cats(), do_action(), clean_bookmark_cache() |
empty($link_id) | 128–137 | wp_parse_args(), sanitize_bookmark(), wp_unslash(), current_time(), gmdate(), get_option(), compact(), compact(), ->update(), __() |
empty($link_id) | 128–138 | wp_parse_args(), sanitize_bookmark(), wp_unslash(), current_time(), gmdate(), get_option(), compact(), ->insert(), wp_set_link_cats(), do_action(), clean_bookmark_cache() |
empty($link_id) && empty($parsed_args) && is_array($link_category) | 128–135 | wp_parse_args(), sanitize_bookmark(), wp_unslash(), get_current_user_id(), current_time(), gmdate(), compact(), compact(), ->update(), wp_set_link_cats(), do_action(), clean_bookmark_cache() |
empty($link_id) && empty($parsed_args) | 129–138 | wp_parse_args(), sanitize_bookmark(), wp_unslash(), get_current_user_id(), current_time(), gmdate(), get_option(), compact(), compact(), ->update(), __() |
empty($link_id) && empty($parsed_args) | 129–139 | wp_parse_args(), sanitize_bookmark(), wp_unslash(), get_current_user_id(), current_time(), gmdate(), get_option(), compact(), ->insert(), wp_set_link_cats(), do_action(), clean_bookmark_cache() |
empty($link_id) | 129–139 | wp_parse_args(), sanitize_bookmark(), wp_unslash(), current_time(), gmdate(), get_option(), compact(), compact(), ->update(), wp_set_link_cats(), do_action(), clean_bookmark_cache() |
empty($link_id) && empty($parsed_args) | 130–140 | wp_parse_args(), sanitize_bookmark(), wp_unslash(), get_current_user_id(), current_time(), gmdate(), get_option(), compact(), compact(), ->update(), wp_set_link_cats(), do_action(), clean_bookmark_cache() |
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 | 216 | 112–139 | 22 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 217 | 112–140 | 22 | |
| 8.4 | 217 | 112–140 | 22 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 223 | 116–144 | 22 | |
| 8.2 | 223 | 116–144 | 22 | |
| 8.1 | 223 | 116–144 | 22 | 10 fewer instructions than PHP 7.4 |
| 7.4 | 233 | 117–150 | 22 |
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.
Hooks and filters fired · 2
2 hooks fire while wp_insert_link() runs, in this order:
- do_action( edit_link )actionline 254 (+80 into the body)
Fires after a link was updated in the database.
- do_action( add_link )actionline 263 (+89 into the body)
Fires after a link was added to the database.
Uses · 11
- wp_parse_args()Merges user defined arguments into defaults array.
- wp_unslash()Removes slashes from a string or recursively removes slashes from strings within an array.
- sanitize_bookmark()Sanitizes all bookmark fields.
- get_current_user_id()Gets the current user's ID.
- current_time()Retrieves the current time based on specified type.
- get_option()Retrieves an option value based on an option name.
- __()Retrieves the translation of $text.
- wp_set_link_cats()Updates link with the specified link categories.
- do_action()Calls the callback functions that have been added to an action hook.
- clean_bookmark_cache()Deletes the bookmark cache.
- WP_Error::__construct()Initializes the error.
Used by · 2
- edit_link()Updates or inserts a link using values provided in $_POST.
- wp_update_link()Updates a link in the database.
Source code
function wp_insert_link( $linkdata, $wp_error = false ) { global $wpdb; $defaults = array( 'link_id' => 0, 'link_name' => '', 'link_url' => '', 'link_rating' => 0, ); $parsed_args = wp_parse_args( $linkdata, $defaults ); $parsed_args = wp_unslash( sanitize_bookmark( $parsed_args, 'db' ) ); $link_id = $parsed_args['link_id']; $link_name = $parsed_args['link_name']; $link_url = $parsed_args['link_url']; $update = false; if ( ! empty( $link_id ) ) { $update = true; } if ( '' === trim( $link_name ) ) { if ( '' !== trim( $link_url ) ) { $link_name = $link_url; } else { return 0; } } if ( '' === trim( $link_url ) ) { return 0; } $link_rating = ( ! empty( $parsed_args['link_rating'] ) ) ? $parsed_args['link_rating'] : 0; $link_image = ( ! empty( $parsed_args['link_image'] ) ) ? $parsed_args['link_image'] : ''; $link_target = ( ! empty( $parsed_args['link_target'] ) ) ? $parsed_args['link_target'] : ''; $link_visible = ( ! empty( $parsed_args['link_visible'] ) ) ? $parsed_args['link_visible'] : 'Y'; $link_owner = ( ! empty( $parsed_args['link_owner'] ) ) ? $parsed_args['link_owner'] : get_current_user_id(); $link_notes = ( ! empty( $parsed_args['link_notes'] ) ) ? $parsed_args['link_notes'] : ''; $link_description = ( ! empty( $parsed_args['link_description'] ) ) ? $parsed_args['link_description'] : ''; $link_rss = ( ! empty( $parsed_args['link_rss'] ) ) ? $parsed_args['link_rss'] : ''; $link_rel = ( ! empty( $parsed_args['link_rel'] ) ) ? $parsed_args['link_rel'] : ''; $link_category = ( ! empty( $parsed_args['link_category'] ) ) ? $parsed_args['link_category'] : array(); $link_updated = gmdate( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) ); // Make sure we set a valid category. if ( ! is_array( $link_category ) || 0 === count( $link_category ) ) { $link_category = array( get_option( 'default_link_category' ) ); } if ( $update ) { if ( false === $wpdb->update( $wpdb->links, compact( 'link_url', 'link_name', 'link_image', 'link_target', 'link_description', 'link_visible', 'link_owner', 'link_rating', 'link_rel', 'link_notes', 'link_rss', 'link_updated' ), compact( 'link_id' ) ) ) { if ( $wp_error ) { return new WP_Error( 'db_update_error', __( 'Could not update link in the database.' ), $wpdb->last_error ); } else { return 0; } } } else { if ( false === $wpdb->insert( $wpdb->links, compact( 'link_url', 'link_name', 'link_image', 'link_target', 'link_description', 'link_visible', 'link_owner', 'link_rating', 'link_rel', 'link_notes', 'link_rss', 'link_updated' ) ) ) { if ( $wp_error ) { return new WP_Error( 'db_insert_error', __( 'Could not insert link into the database.' ), $wpdb->last_error ); } else { return 0; } } $link_id = (int) $wpdb->insert_id; } wp_set_link_cats( $link_id, $link_category ); if ( $update ) { /** * Fires after a link was updated in the database. * * @since 2.0.0 * * @param int $link_id ID of the link that was updated. */Changelog
Introduced in 2.0.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 7.1.0 tag, from
src/wp-admin/includes/bookmark.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.