1

I have a form on my site.

This the user register one missing person. I created a custom post type missing person, custom taxonomies, and custom fields. All this input's save as well in wp.

But I don't know submit an image and save this image as featured image.

$my_post = array(
                 'post_title' => $_POST['title'],
                 'post_date' => $_SESSION['cal_startdate'],
                 'post_content' => $_POST['myContent'],
                 'post_status' => 'draft',
                 'post_type' => 'pessoa_desaparecida',
              );

$post_id = wp_insert_post($my_post);


$uploaddir = wp_upload_dir();
$file = $_FILES["test"]["name"];
$uploadfile = $uploaddir['path'] . '/' . basename( $file );

move_uploaded_file( $file , $uploadfile );
$filename = basename( $uploadfile );

$wp_filetype = wp_check_filetype(basename($filename), null );

$attachment = array(
    'post_mime_type' => $wp_filetype['type'],
    'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
    'post_content' => '',
    'post_status' => 'inherit',
    'menu_order' => $_i + 1000
);
$attach_id = wp_insert_attachment( $attachment, $uploadfile );
update_post_meta($post_id,'_thumbnail_id',$attach_id);

set_post_thumbnail( $post_id, $thumbnail_id );

but not works

My Form in frontend is:

<form action="/homolog/cadastro" method="post" enctype="multipart/form-data">
 <input type="text"  name="title" />
 <input type="file" size="20"  name="test" />
</form>

PRINT:

enter image description here

enter image description here

  • I have the form in my frontend, this form insert a custom post type. This form has one input file to upload a image, this image would be my featured image. I just upload and set one featured image from my form – Arie Jul 21 '14 at 1:45
  • If you want to get help you need to edit your question and describe what have you tried and where did it fail. Right now you didn't describe any problem, you are on the right way you just need to do some research and use the relevant APIs – Mark Kaplun Jul 21 '14 at 1:55
  • Mark, I change the question. Do you can help? – Arie Jul 21 '14 at 2:37
  • possible duplicate of Set Featured Image Front Frontend Form – Brad Dalton Jul 21 '14 at 17:57
  • Brad, my folder uploads/2014/07/ is empty! – Arie Jul 21 '14 at 18:24
1

sorry this isn't an answer but you need to sanitize that user input. Your allowing tags to be written directly into your post content which allows anyone to run javascript in both wp admin and on the front end of your site (assuming you'll eventually print these posts). highly recommend (at minimum) strip_tags() or htmlspecialchars() on all user input strings. there are better/alternative ways but this is at least a good start without going into too much detail.

In theory the title would be an issue but I'm pretty sure WordPress will strip tags from there. however WordPress allows script tags in post content so that people can use embed codes.

if you are unaware of the risks of not sanitizing user input then id recommend even more caution when handling file uploads. downloading a malicious file or having thousands of your websites users do this could potentially be alot worse than having some malicious javascript run inside your browser. Thankfully wp_insert_post will at least prevent sql injection on your completely raw user input.

my only further recommendation is to at least check the allowed mime types and use WordPress functions whenever possible to handle the uploads, assuming that they might offer some protection against malicious files but apart from that i can't give much advice on how to do this securely (although i have done it before... i don't have access to that code right now).

  • even if this is not a direct answer I would draw a fat red border around it. you can't pay enough attention on user input! – Pat_Morita Apr 6 '19 at 19:25
0

You can try this way :

set_post_thumbnail( $my_post_id, $thumbnail_id );

You have to add image into library first like this way :

 $uploaddir = wp_upload_dir();
 $file = $_FILES[ ... whatever you have in your POST data ... ];
 $uploadfile = $uploaddir['path'] . '/' . basename( $file );

 move_uploaded_file( $file , $uploadfile );
 $filename = basename( $uploadfile );

 $wp_filetype = wp_check_filetype(basename($filename), null );

 $attachment = array(
      'post_mime_type' => $wp_filetype['type'],
      'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
      'post_content' => '',
      'post_status' => 'inherit',
      'menu_order' => $_i + 1000
  );
  $attach_id = wp_insert_attachment( $attachment, $uploadfile );

For more detail please see this link

  • Hi Bindiya, I tested, but not works. The script create the media but not save the file in uploads. If the file has in the folder, is show in the Media Gallery, less in thumb area. – Arie Jul 21 '14 at 17:10
0

First of all you should check if your theme supports thumbnails and that your missing person CPT supports it too. If you create your CPT via PHP coding, check this:

add_theme_support( 'post-thumbnails' );
register_post_type( 'yourposttype', array(
    ...,
    'supports' => array('title', ...,'thumbnail'),
));

}

Then if this is right, the code in the answer below should work.

0

In your case, I'll prefer suggest you to use one of these functions depending on the case and especially what you get from $_POST :

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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