返回介绍

wp_insert_link()

发布于 2017-09-11 12:14:14 字数 6363 浏览 1253 评论 0 收藏 0

wp_insert_link( array $linkdata,  bool $wp_error = false )

Inserts/updates links into/in the database.


description


参数

$linkdata

(array) (Required) Elements that make up the link to insert.

$wp_error

(bool) (Optional) Whether to return a WP_Error object on failure.

Default value: false


返回值

(int|WP_Error) Value 0 or WP_Error on failure. The link ID on success.


源代码

File: wp-admin/includes/bookmark.php

function wp_insert_link( $linkdata, $wp_error = false ) {
	global $wpdb;

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

	$args = wp_parse_args( $linkdata, $defaults );
	$r = wp_unslash( sanitize_bookmark( $args, 'db' ) );

	$link_id   = $r['link_id'];
	$link_name = $r['link_name'];
	$link_url  = $r['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( $r['link_rating'] ) ) ? $r['link_rating'] : 0;
	$link_image       = ( ! empty( $r['link_image'] ) ) ? $r['link_image'] : '';
	$link_target      = ( ! empty( $r['link_target'] ) ) ? $r['link_target'] : '';
	$link_visible     = ( ! empty( $r['link_visible'] ) ) ? $r['link_visible'] : 'Y';
	$link_owner       = ( ! empty( $r['link_owner'] ) ) ? $r['link_owner'] : get_current_user_id();
	$link_notes       = ( ! empty( $r['link_notes'] ) ) ? $r['link_notes'] : '';
	$link_description = ( ! empty( $r['link_description'] ) ) ? $r['link_description'] : '';
	$link_rss         = ( ! empty( $r['link_rss'] ) ) ? $r['link_rss'] : '';
	$link_rel         = ( ! empty( $r['link_rel'] ) ) ? $r['link_rel'] : '';
	$link_category    = ( ! empty( $r['link_category'] ) ) ? $r['link_category'] : array();

	// 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_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 ) {
		/**
		 * Fires after a link was updated in the database.
		 *
		 * @since 2.0.0
		 *
		 * @param int $link_id ID of the link that was updated.
		 */
		do_action( 'edit_link', $link_id );
	} else {
		/**
		 * Fires after a link was added to the database.
		 *
		 * @since 2.0.0
		 *
		 * @param int $link_id ID of the link that was added.
		 */
		do_action( 'add_link', $link_id );
	}
	clean_bookmark_cache( $link_id );

	return $link_id;
}

更新日志

Versiondescription
2.0.0Introduced.

相关函数

Uses

  • wp-admin/includes/bookmark.php: wp_set_link_cats()
  • wp-admin/includes/bookmark.php: edit_link
  • wp-admin/includes/bookmark.php: add_link
  • wp-includes/l10n.php: __()
  • wp-includes/formatting.php: wp_unslash()
  • wp-includes/functions.php: wp_parse_args()
  • wp-includes/plugin.php: do_action()
  • wp-includes/option.php: get_option()
  • wp-includes/user.php: get_current_user_id()
  • wp-includes/bookmark.php: sanitize_bookmark()
  • wp-includes/bookmark.php: clean_bookmark_cache()
  • wp-includes/wp-db.php: wpdb::update()
  • wp-includes/wp-db.php: wpdb::insert()
  • wp-includes/class-wp-error.php: WP_Error::__construct()
  • Show 9 more uses Hide more uses

Used By

  • wp-admin/includes/bookmark.php: wp_update_link()
  • wp-admin/includes/bookmark.php: edit_link()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by waynestiles

    Since WP 4.4 the insert link command is no longer shift-option A

    What is it?

  2. Basic Usage

    wp_insert_link() requires an array of arguments. Only two of the arguments must be provided – link_name and link_url. The rest are optional.

    
    <?php
        $linkdata = array(
    	'link_name' => 'WordPress Code Reference',
    	'link_url' => 'https://developer.wordpress.org/reference'
        );
    
        $link_id = wp_insert_link( $linkdata );
    ?>
    

    Update a link

    wp_insert_link() will update a link – instead of inserting it – if you provide the link’s ID.

    
    <?php
        $linkdata = array(
    	'link_id'   => 7,
    	'link_name' => 'WordPress Code Reference',
    	'link_url'  => 'https://developer.wordpress.org/reference'
        );
    
        $link_id = wp_insert_link( $linkdata );
    ?>
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文