wp_insert_link

Inserts/updates links into/in the database.

Description

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

Returns (int|WP_Error)

Value 0 or WP_Error on failure. The link ID on success.

Parameters (2)

0. $linkdata (array)
Elements that make up the link to insert.
1. $wp_error — Optional. (bool) => false
Whether to return a WP_Error object on failure. Default false.

Usage

  1. if ( !function_exists( 'wp_insert_link' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/bookmark.php'; 
  3.  
  4. // Elements that make up the link to insert. 
  5. $linkdata = array(); 
  6.  
  7. // Optional. Whether to return a WP_Error object on failure. Default false. 
  8. $wp_error = false; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wp_insert_link($linkdata, $wp_error); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/bookmark.php  
  1. function wp_insert_link( $linkdata, $wp_error = false ) { 
  2. global $wpdb; 
  3.  
  4. $defaults = array( 'link_id' => 0, 'link_name' => '', 'link_url' => '', 'link_rating' => 0 ); 
  5.  
  6. $args = wp_parse_args( $linkdata, $defaults ); 
  7. $r = wp_unslash( sanitize_bookmark( $args, 'db' ) ); 
  8.  
  9. $link_id = $r['link_id']; 
  10. $link_name = $r['link_name']; 
  11. $link_url = $r['link_url']; 
  12.  
  13. $update = false; 
  14. if ( ! empty( $link_id ) ) { 
  15. $update = true; 
  16.  
  17. if ( trim( $link_name ) == '' ) { 
  18. if ( trim( $link_url ) != '' ) { 
  19. $link_name = $link_url; 
  20. } else { 
  21. return 0; 
  22.  
  23. if ( trim( $link_url ) == '' ) { 
  24. return 0; 
  25.  
  26. $link_rating = ( ! empty( $r['link_rating'] ) ) ? $r['link_rating'] : 0; 
  27. $link_image = ( ! empty( $r['link_image'] ) ) ? $r['link_image'] : ''; 
  28. $link_target = ( ! empty( $r['link_target'] ) ) ? $r['link_target'] : ''; 
  29. $link_visible = ( ! empty( $r['link_visible'] ) ) ? $r['link_visible'] : 'Y'; 
  30. $link_owner = ( ! empty( $r['link_owner'] ) ) ? $r['link_owner'] : get_current_user_id(); 
  31. $link_notes = ( ! empty( $r['link_notes'] ) ) ? $r['link_notes'] : ''; 
  32. $link_description = ( ! empty( $r['link_description'] ) ) ? $r['link_description'] : ''; 
  33. $link_rss = ( ! empty( $r['link_rss'] ) ) ? $r['link_rss'] : ''; 
  34. $link_rel = ( ! empty( $r['link_rel'] ) ) ? $r['link_rel'] : ''; 
  35. $link_category = ( ! empty( $r['link_category'] ) ) ? $r['link_category'] : array(); 
  36.  
  37. // Make sure we set a valid category. 
  38. if ( ! is_array( $link_category ) || 0 == count( $link_category ) ) { 
  39. $link_category = array( get_option( 'default_link_category' ) ); 
  40.  
  41. if ( $update ) { 
  42. if ( false === $wpdb->update( $wpdb->links, compact( 'link_url', 'link_name', 'link_image', 'link_target', 'link_description', 'link_visible', 'link_rating', 'link_rel', 'link_notes', 'link_rss' ), compact( 'link_id' ) ) ) { 
  43. if ( $wp_error ) { 
  44. return new WP_Error( 'db_update_error', __( 'Could not update link in the database' ), $wpdb->last_error ); 
  45. } else { 
  46. return 0; 
  47. } else { 
  48. 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' ) ) ) { 
  49. if ( $wp_error ) { 
  50. return new WP_Error( 'db_insert_error', __( 'Could not insert link into the database' ), $wpdb->last_error ); 
  51. } else { 
  52. return 0; 
  53. $link_id = (int) $wpdb->insert_id; 
  54.  
  55. wp_set_link_cats( $link_id, $link_category ); 
  56.  
  57. if ( $update ) { 
  58. /** 
  59. * Fires after a link was updated in the database. 
  60. * @since 2.0.0 
  61. * @param int $link_id ID of the link that was updated. 
  62. */ 
  63. do_action( 'edit_link', $link_id ); 
  64. } else { 
  65. /** 
  66. * Fires after a link was added to the database. 
  67. * @since 2.0.0 
  68. * @param int $link_id ID of the link that was added. 
  69. */ 
  70. do_action( 'add_link', $link_id ); 
  71. clean_bookmark_cache( $link_id ); 
  72.  
  73. return $link_id;