admin管理员组

文章数量:1419190

I want to insert newpost on Front-end of wordpress.
And i want to rename images name during upload for my code below
Result like: "MemberUpload_dd/mm/yy_random_number_[width]x[height].jpg"
(For all size: thumbnail, medium, large, full and custom size) My code upload i'm using for my theme..
Thanks friends verymuch! ;)

// Upload 
if (!function_exists('wp_generate_attachment_metadata')){
                    require_once(ABSPATH . "wp-admin" . '/includes/image.php');
                    require_once(ABSPATH . "wp-admin" . '/includes/file.php');
                    require_once(ABSPATH . "wp-admin" . '/includes/media.php');
                }
                 if ($_FILES) {
                    foreach ($_FILES as $file => $array) {
                        if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
                            return "Upload Err: " . $_FILES[$file]['error'];
                        }
                        $attach_id = media_handle_upload( $file, $newPost );
                    }   
                }
                if ($attach_id > 0){
                    //and if you want to set that image as Post  then use:
                    update_post_meta($newPost,'Uploaded_img_id',$attach_id);
                }
// END upload

I want to insert newpost on Front-end of wordpress.
And i want to rename images name during upload for my code below
Result like: "MemberUpload_dd/mm/yy_random_number_[width]x[height].jpg"
(For all size: thumbnail, medium, large, full and custom size) My code upload i'm using for my theme..
Thanks friends verymuch! ;)

// Upload 
if (!function_exists('wp_generate_attachment_metadata')){
                    require_once(ABSPATH . "wp-admin" . '/includes/image.php');
                    require_once(ABSPATH . "wp-admin" . '/includes/file.php');
                    require_once(ABSPATH . "wp-admin" . '/includes/media.php');
                }
                 if ($_FILES) {
                    foreach ($_FILES as $file => $array) {
                        if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
                            return "Upload Err: " . $_FILES[$file]['error'];
                        }
                        $attach_id = media_handle_upload( $file, $newPost );
                    }   
                }
                if ($attach_id > 0){
                    //and if you want to set that image as Post  then use:
                    update_post_meta($newPost,'Uploaded_img_id',$attach_id);
                }
// END upload
Share Improve this question edited Jun 14, 2013 at 18:27 Zen Nguyễn asked Jun 14, 2013 at 16:13 Zen NguyễnZen Nguyễn 474 silver badges11 bronze badges 4
  • 2 you can't put slashes in a filename, it is a path separator. see this question for a potential answer. – Milo Commented Jun 14, 2013 at 16:39
  • @Milo : It's not work for me at front-end posting :( It only Work when i'm upload picture in Dashboard. – Zen Nguyễn Commented Jun 14, 2013 at 18:25
  • This post may help you [enter link description here][1] [1]: wordpress.stackexchange/q/93995/37854 – usama sulaiman Commented Oct 1, 2013 at 16:02
  • Your syntax is bad. – Nathan Powell Commented Jul 27, 2019 at 5:15
Add a comment  | 

1 Answer 1

Reset to default -1

I think You need to Know about using PHP to upload files as well

PHP File Upload

本文标签: Rename upload images during upload