1

I create post page from front end , and i add code for uplaod feature image. I use this code

    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) $error_array[]='STOP';
                $attach_id = media_handle_upload( $file, $post_id );
            }   
        }
        if ($attach_id > 0){
            //and if you want to set that image as Post  then use:
            update_post_meta($post_id,'_thumbnail_id',$attach_id);
        }

It's work. But i want upload only image

How can i do it ?

5
  • and what does this let you upload?
    – vimes1984
    Sep 28, 2014 at 17:17
  • the code is upload multi files type [pdf / jpg / rar ... etc] i want upload only image
    – LET ME
    Sep 28, 2014 at 18:03
  • check the extension on the front end via JS...| Or check the $_FILES array prior to sending it to media_handle_upload() do you have an example of a var_dump of the $files array?
    – vimes1984
    Sep 28, 2014 at 18:24
  • yes i want example .. iam not developer
    – LET ME
    Sep 28, 2014 at 19:43
  • I'm asking do YOU have an example... Ok do this: var_dump($_FILES); and tell us the output...
    – vimes1984
    Sep 28, 2014 at 20:03

2 Answers 2

2

I edited code

    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) {
            $mime = $_FILES[$file]['type'];
            $filesize = $_FILES[$file]['size'];
            $maxsizef = 524288;
            if($filesize > $maxsizef) $error_array[] = 'error size, max file size = 500 KB';
            if(($mime != 'image/jpeg') && ($mime != 'image/jpg') && ($mime != 'image/png')) $error_array[] ='error type , please upload: jpg, jpeg, png';
                $attach_id = media_handle_upload( $file, $post_id );
            }   
        }
        if ($attach_id > 0){
            //and if you want to set that image as Post  then use:
            update_post_meta($post_id,'_thumbnail_id',$attach_id);
        }

it's work.

0

I guess the answer to your question is really well documented in the code you're using:

 //and if you want to set that image as Post  then use:
 update_post_meta($post_id,'_thumbnail_id',$attach_id);

So all you have to do is to comment the following line: update_post_meta($post_id,'_thumbnail_id',$attach_id);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.