admin管理员组文章数量:1130349
Im using wp_insert_post to add products on the front end of a woocommerces site
My current code will upload all the images but only the last image will be in the product gallery images
heres my code;
functions.php
function my_handle_attachment( $file_handler, $post_id, $set_thu=false) {
// check to make sure its a successful upload
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$attach_id = media_handle_upload( $file_handler, $post_id );
if ( is_numeric( $attach_id ) ) {
update_post_meta( $post_id, '_product_image_gallery', $attach_id );
}
return $attach_id;
}
frontend
if ( $_FILES ) {
$files = $_FILES['upload_attachment'];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array("upload_attachment" => $file);
foreach ($_FILES as $file => $array) {
$newupload = my_handle_attachment($file,$post_id);
update_post_meta($post_id, array_push($post_id, '_product_image_gallery',$newupload));
}
}
}
}
<input type="file" name="upload_attachment[]" multiple="multiple" />
Is anything wrong in this code ?
Im using wp_insert_post to add products on the front end of a woocommerces site
My current code will upload all the images but only the last image will be in the product gallery images
heres my code;
functions.php
function my_handle_attachment( $file_handler, $post_id, $set_thu=false) {
// check to make sure its a successful upload
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$attach_id = media_handle_upload( $file_handler, $post_id );
if ( is_numeric( $attach_id ) ) {
update_post_meta( $post_id, '_product_image_gallery', $attach_id );
}
return $attach_id;
}
frontend
if ( $_FILES ) {
$files = $_FILES['upload_attachment'];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array("upload_attachment" => $file);
foreach ($_FILES as $file => $array) {
$newupload = my_handle_attachment($file,$post_id);
update_post_meta($post_id, array_push($post_id, '_product_image_gallery',$newupload));
}
}
}
}
<input type="file" name="upload_attachment[]" multiple="multiple" />
Is anything wrong in this code ?
Share Improve this question asked Nov 28, 2018 at 5:41 Tejas GajjarTejas Gajjar 584 bronze badges2 Answers
Reset to default 1Hello Tejas you may please alter your code below and let me know if it worked for you .
foreach ($files['name'] as $key => $value) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array ("files" => $file);
foreach ($_FILES as $file => $array) {
$newupload = my_handle_attachment($file,$post_id);
}
array_push($gallery,$newupload);
}
update_post_meta($post_id,'_product_image_gallery', implode(',',$gallery));
you can clean out the title attributes yourself by adding the following to your functions.php file:
function remove_title_attributes($input) {
return preg_replace('/\s*title\s*=\s*(["\']).*?\1/', '', $input);
}
add_filter( 'wp_list_pages', 'remove_title_attributes' );
Im using wp_insert_post to add products on the front end of a woocommerces site
My current code will upload all the images but only the last image will be in the product gallery images
heres my code;
functions.php
function my_handle_attachment( $file_handler, $post_id, $set_thu=false) {
// check to make sure its a successful upload
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$attach_id = media_handle_upload( $file_handler, $post_id );
if ( is_numeric( $attach_id ) ) {
update_post_meta( $post_id, '_product_image_gallery', $attach_id );
}
return $attach_id;
}
frontend
if ( $_FILES ) {
$files = $_FILES['upload_attachment'];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array("upload_attachment" => $file);
foreach ($_FILES as $file => $array) {
$newupload = my_handle_attachment($file,$post_id);
update_post_meta($post_id, array_push($post_id, '_product_image_gallery',$newupload));
}
}
}
}
<input type="file" name="upload_attachment[]" multiple="multiple" />
Is anything wrong in this code ?
Im using wp_insert_post to add products on the front end of a woocommerces site
My current code will upload all the images but only the last image will be in the product gallery images
heres my code;
functions.php
function my_handle_attachment( $file_handler, $post_id, $set_thu=false) {
// check to make sure its a successful upload
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$attach_id = media_handle_upload( $file_handler, $post_id );
if ( is_numeric( $attach_id ) ) {
update_post_meta( $post_id, '_product_image_gallery', $attach_id );
}
return $attach_id;
}
frontend
if ( $_FILES ) {
$files = $_FILES['upload_attachment'];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array("upload_attachment" => $file);
foreach ($_FILES as $file => $array) {
$newupload = my_handle_attachment($file,$post_id);
update_post_meta($post_id, array_push($post_id, '_product_image_gallery',$newupload));
}
}
}
}
<input type="file" name="upload_attachment[]" multiple="multiple" />
Is anything wrong in this code ?
Share Improve this question asked Nov 28, 2018 at 5:41 Tejas GajjarTejas Gajjar 584 bronze badges2 Answers
Reset to default 1Hello Tejas you may please alter your code below and let me know if it worked for you .
foreach ($files['name'] as $key => $value) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array ("files" => $file);
foreach ($_FILES as $file => $array) {
$newupload = my_handle_attachment($file,$post_id);
}
array_push($gallery,$newupload);
}
update_post_meta($post_id,'_product_image_gallery', implode(',',$gallery));
you can clean out the title attributes yourself by adding the following to your functions.php file:
function remove_title_attributes($input) {
return preg_replace('/\s*title\s*=\s*(["\']).*?\1/', '', $input);
}
add_filter( 'wp_list_pages', 'remove_title_attributes' );
本文标签: woocommerce offtopicHow to upload multiple image using wpinsertpost
版权声明:本文标题:woocommerce offtopic - How to upload multiple image using wp_insert_post 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749145691a2322950.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论