Сохранить миниатюру продукта woocommerce как url, а не attachchement_id? - PullRequest
0 голосов
/ 20 апреля 2020

Попытка загрузить изображение со страницы продукта woocommerce и установить его в качестве эскиза в сетке Reactive Pro с помощью set_post_thumbnail. Ссылка, полученная в списке товаров, использует attachchement_id вместо абсолютного пути к изображению. Есть ли способ установить чистый URL-адрес вместо идентификатора вложения или изменить сетку Reactive Pro, чтобы получить миниатюру по URL-адресу вместо php?

<form method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload">
<input type="submit" name="submit" value="Update"/>
</form>

<?php

global $product;
$id = $product->get_id();
if ($_POST['submit'] == "Update")
{
$target_dir = "wp-content/uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
$upload_file = wp_upload_bits( $target_file, null, @file_get_contents( $target_file) );

if ( ! $upload_file['error'] ) {
  // if succesfull insert the new file into the media library (create a new attachment post type).
  $wp_filetype = wp_check_filetype($target_file, null );

  $attachment = array(
    'post_mime_type' => $wp_filetype['type'],
    'post_parent'    => $id,
    'post_title'     => preg_replace( '/\.[^.]+$/', '', $target_file ),
    'post_content'   => '',
    'post_status'    => 'inherit'
  );

  $attachment_id = wp_insert_attachment( $attachment, $upload_file['file'], $id );

  if ( ! is_wp_error( $attachment_id ) ) {
     // if attachment post was successfully created, insert it as a thumbnail to the post $post_id.
     require_once(ABSPATH . "wp-admin" . '/includes/image.php');

     $attachment_data = wp_generate_attachment_metadata( $attachment_id, $upload_file['file'] );

     wp_update_attachment_metadata( $attachment_id,  $attachment_data );
      update_term_meta( $id, 'thumbnail_id', absint( $attachment_id ) );
     set_post_thumbnail( $id, $attachment_id );
   }
}
}

?>
<img src="<?php echo get_the_post_thumbnail_url($id);?>" />
...