wp_insert_link

Definition:
function wp_insert_link( $linkdata, $wp_error = false ) {}

This function inserts/updates links into/in the database.

Parameters

  • array $linkdata: Elements that make up the link to insert.
  • bool $wp_error: Optional. If true return WP_Error object on failure.

Return values

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

Defined actions

  • edit_link
    do_action( 'edit_link', $link_id );
  • add_link
    do_action( 'add_link', $link_id );

Source code

function wp_insert_link( $linkdata, $wp_error = false ) {

	global $wpdb;



	$defaults = array( 'link_id' => 0, 'link_name' => '', 'link_url' => '', 'link_rating' => 0 );



	$linkdata = wp_parse_args( $linkdata, $defaults );

	$linkdata = sanitize_bookmark( $linkdata, 'db' );



	extract( stripslashes_deep( $linkdata ), EXTR_SKIP );



	$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;



	if ( empty( $link_rating ) )

		$link_rating = 0;



	if ( empty( $link_image ) )

		$link_image = '';



	if ( empty( $link_target ) )

		$link_target = '';



	if ( empty( $link_visible ) )

		$link_visible = 'Y';



	if ( empty( $link_owner ) )

		$link_owner = get_current_user_id();



	if ( empty( $link_notes ) )

		$link_notes = '';



	if ( empty( $link_description ) )

		$link_description = '';



	if ( empty( $link_rss ) )

		$link_rss = '';



	if ( empty( $link_rel ) )

		$link_rel = '';



	// Make sure we set a valid category

	if ( ! isset( $link_category ) || 0 == count( $link_category ) || !is_array( $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_rating', 'link_rel', 'link_notes', 'link_rss'), 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') ) ) {

			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 )

		do_action( 'edit_link', $link_id );

	else

		do_action( 'add_link', $link_id );



	clean_bookmark_cache( $link_id );



	return $link_id;

}

3791

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: