返回介绍

wp_media_attach_action()

发布于 2017-09-11 12:26:54 字数 2899 浏览 958 评论 0 收藏 0

wp_media_attach_action( int $parent_id,  string $action = 'attach' )

Encapsulate logic for Attach/Detach actions


description


参数

$parent_id

(int) (Required) Attachment parent ID.

$action

(string) (Optional) Attach/detach action. Accepts 'attach' or 'detach'.

Default value: 'attach'


源代码

File: wp-admin/includes/media.php

function wp_media_attach_action( $parent_id, $action = 'attach' ) {
	global $wpdb;

	if ( ! $parent_id ) {
		return;
	}

	if ( ! current_user_can( 'edit_post', $parent_id ) ) {
		wp_die( __( 'Sorry, you are not allowed to edit this post.' ) );
	}
	$ids = array();
	foreach ( (array) $_REQUEST['media'] as $att_id ) {
		$att_id = (int) $att_id;

		if ( ! current_user_can( 'edit_post', $att_id ) ) {
			continue;
		}

		$ids[] = $att_id;
	}

	if ( ! empty( $ids ) ) {
		$ids_string = implode( ',', $ids );
		if ( 'attach' === $action ) {
			$result = $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_parent = %d WHERE post_type = 'attachment' AND ID IN ( $ids_string )", $parent_id ) );
		} else {
			$result = $wpdb->query( "UPDATE $wpdb->posts SET post_parent = 0 WHERE post_type = 'attachment' AND ID IN ( $ids_string )" );
		}

		foreach ( $ids as $att_id ) {
			clean_attachment_cache( $att_id );
		}
	}

	if ( isset( $result ) ) {
		$location = 'upload.php';
		if ( $referer = wp_get_referer() ) {
			if ( false !== strpos( $referer, 'upload.php' ) ) {
				$location = remove_query_arg( array( 'attached', 'detach' ), $referer );
			}
		}

		$key = 'attach' === $action ? 'attached' : 'detach';
		$location = add_query_arg( array( $key => $result ), $location );
		wp_redirect( $location );
		exit;
	}
}

更新日志

Versiondescription
4.2.0Introduced.

相关函数

Uses

  • wp-includes/capabilities.php: current_user_can()
  • wp-includes/l10n.php: __()
  • wp-includes/pluggable.php: wp_redirect()
  • wp-includes/functions.php: wp_die()
  • wp-includes/functions.php: wp_get_referer()
  • wp-includes/functions.php: remove_query_arg()
  • wp-includes/functions.php: add_query_arg()
  • wp-includes/post.php: clean_attachment_cache()
  • wp-includes/wp-db.php: wpdb::query()
  • wp-includes/wp-db.php: wpdb::prepare()
  • Show 5 more uses Hide more uses

User Contributed Notes

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

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

发布评论

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