wppaste
WordPress

wp_insert_link( array $linkdata, bool $wp_error = false ): int|WP_Error

Since
2.0.0
Source
wp-admin/includes/bookmark.php:174
Inserts a link into the database, or updates an existing link.

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_idint

    Optional. The ID of the existing link if updating.
  • $link_urlstring

    The URL the link points to.
  • $link_namestring

    The title of the link.
  • $link_imagestring

    Optional. A URL of an image.
  • $link_targetstring

    Optional. The target element for the anchor tag.
  • $link_descriptionstring

    Optional. A short description of the link.
  • $link_visiblestring

    Optional. 'Y' means visible, anything else means not.
  • $link_ownerint

    Optional. A user ID.
  • $link_ratingint

    Optional. A rating for the link.
  • $link_relstring

    Optional. A relationship of the link to you.
  • $link_notesstring

    Optional. An extended description of or notes on the link.
  • $link_rssstring

    Optional. A URL of an associated RSS feed.
  • $link_categoryint

    Optional. 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
Value 0 or WP_Error on failure. The link ID on success.

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

Reaches the database via ->insert().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
112–140

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 217.

Plugin surface
2 hooks

Third-party callbacks on 'edit_link', 'add_link' run inside this call, and their cost is not bounded by anything here.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • optionoption read or writeget_option()called directly
  • hookthird-party callbacksdo_action()called directly
  • sqldatabase query->insert()called directly
  • cacheobject cachewp_cache_get()one call below wp_insert_link()
  • serializeserialisationmaybe_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.

WhenInstructionsCalls it makes
empty($link_id) && is_array($link_category)112–118wp_parse_args(), sanitize_bookmark(), wp_unslash(), current_time(), gmdate(), compact(), ->insert()
empty($link_id) && empty($parsed_args) && is_array($link_category)113–119wp_parse_args(), sanitize_bookmark(), wp_unslash(), get_current_user_id(), current_time(), gmdate(), compact(), ->insert()
empty($link_id)114–123wp_parse_args(), sanitize_bookmark(), wp_unslash(), current_time(), gmdate(), get_option(), compact(), ->insert()
empty($link_id) && empty($parsed_args)115–124wp_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–122wp_parse_args(), sanitize_bookmark(), wp_unslash(), current_time(), gmdate(), compact(), compact(), ->update()
empty($link_id) && empty($parsed_args) && is_array($link_category)117–123wp_parse_args(), sanitize_bookmark(), wp_unslash(), get_current_user_id(), current_time(), gmdate(), compact(), compact(), ->update()
empty($link_id)118–127wp_parse_args(), sanitize_bookmark(), wp_unslash(), current_time(), gmdate(), get_option(), compact(), compact(), ->update()
empty($link_id) && empty($parsed_args)119–128wp_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–128wp_parse_args(), sanitize_bookmark(), wp_unslash(), current_time(), gmdate(), compact(), ->insert(), __()
empty($link_id) && empty($parsed_args) && is_array($link_category)123–129wp_parse_args(), sanitize_bookmark(), wp_unslash(), get_current_user_id(), current_time(), gmdate(), compact(), ->insert(), __()
empty($link_id)124–133wp_parse_args(), sanitize_bookmark(), wp_unslash(), current_time(), gmdate(), get_option(), compact(), ->insert(), __()
empty($link_id) && empty($parsed_args)125–134wp_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–132wp_parse_args(), sanitize_bookmark(), wp_unslash(), current_time(), gmdate(), compact(), compact(), ->update(), __()
empty($link_id) && is_array($link_category)126–133wp_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–133wp_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–134wp_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–134wp_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–137wp_parse_args(), sanitize_bookmark(), wp_unslash(), current_time(), gmdate(), get_option(), compact(), compact(), ->update(), __()
empty($link_id)128–138wp_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–135wp_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–138wp_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–139wp_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–139wp_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–140wp_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

PHPCompiledExecutedBranchesNotes
8.6-dev216112–139221 fewer instruction than PHP 8.5
8.5217112–14022
8.4217112–140226 fewer instructions than PHP 8.3
8.3223116–14422
8.2223116–14422
8.1223116–1442210 fewer instructions than PHP 7.4
7.4233117–15022

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:

  1. do_action( edit_link )actionline 254 (+80 into the body)

    Fires after a link was updated in the database.

  2. do_action( add_link )actionline 263 (+89 into the body)

    Fires after a link was added to the database.

Uses · 11

Used by · 2

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 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.