تابع wc_rest_set_uploaded_image_as_attachment افزودن تصویر آپلود شده به رسانه

تابع ووکامرسی wc_rest_set_uploaded_image_as_attachment – افزودن تصویر آپلود شده به رسانه
Syntax – سینتکس
(int) wc_rest_set_uploaded_image_as_attachment( (array) $upload, (int) $id = 0 );
Parameters – پارامتر ها (2)
- 1- $upload (array)
- 2- $id (int)
Returns – مقادیر بازگشتی (int)
Attachment ID
Usage – نحوه استفاده
if ( !function_exists( 'wc_rest_set_uploaded_image_as_attachment' ) ) {
require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-rest-functions.php';
}
// Upload information from wp_upload_bits.
$upload = array();
// Post ID. Default to 0.
$id = -1;
// NOTICE! Understand what this does before running.
$result = wc_rest_set_uploaded_image_as_attachment($upload, $id);
Defined – محل تعریف
/includes/wc-rest-functions.php
function wc_rest_set_uploaded_image_as_attachment( $upload, $id = 0 ) {
$info = wp_check_filetype( $upload['file'] );
$title = '';
$content = '';
if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) {
include_once( ABSPATH . 'wp-admin/includes/image.php' );
}
if ( $image_meta = wp_read_image_metadata( $upload['file'] ) ) {
if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) {
$title = wc_clean( $image_meta['title'] );
}
if ( trim( $image_meta['caption'] ) ) {
$content = wc_clean( $image_meta['caption'] );
}
}
$attachment = array(
'post_mime_type' => $info['type'],
'guid' => $upload['url'],
'post_parent' => $id,
'post_title' => $title,
'post_content' => $content,
);
$attachment_id = wp_insert_attachment( $attachment, $upload['file'], $id );
if ( ! is_wp_error( $attachment_id ) ) {
wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $upload['file'] ) );
}
return $attachment_id;
}
versions – نسخه ها
از نسخه : 2.6.0
نسخه فعلی : 3.0.6
دیگر نسخه ها : 3.0.6 , 3.0.5 , 3.0.4 , 3.0.3 , 3.0.2
ارسال نظر