Soumettre le message et télécharger l'image depuis le front-end
2 réponses
- votes
-
- 2011-03-13
Si vousparlez de la réponse,j'aipublié ici il suffit detélécharger unfichier dans uneiframepour obtenir une soumission "Ajax like".
Maintenant,si vous avez déjà unformulaire quigère l'envoi demessages,vouspouvez simplement ajouter le champ detéléchargement dufichier quelquepart dans votreformulaire:
<form ... ... <input type="file" name="thumbnail" id="thumbnail"> ... ... </form>
assurez-vous que votreformulaire a l'attribut
enctype="multipart/form-data"
.puis dans votre script detraitement deformulaire après avoir créé l'article (en supposant que vous utilisez
wp_insert_post();
) conserver l'identifiant depublication dans unenouvelle variable:$new_post = wp_insert_post($post_array);
et après cela,ajoutez:
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 error : " . $_FILES[$file]['error']; } $attach_id = media_handle_upload( $file, $new_post ); } } if ($attach_id > 0){ //and if you want to set that image as Post then use: update_post_meta($new_post,'_thumbnail_id',$attach_id); }
et votreimage seratéléverséeet enregistréeen tant queminiature depublication.
If you are talking about the answer i posted here its simply uploading file in an iframe to achieve "Ajax like" submit.
Now if you already have a form that handles the post submit you can simply add the upload file field input somewhere in your form:
<form ... ... <input type="file" name="thumbnail" id="thumbnail"> ... ... </form>
make sure that your form has
enctype="multipart/form-data"
attribute.then in your form processing script after you create the post (assuming that you are using
wp_insert_post();
) keep hold of the post ID in a new var:$new_post = wp_insert_post($post_array);
and after that add:
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 error : " . $_FILES[$file]['error']; } $attach_id = media_handle_upload( $file, $new_post ); } } if ($attach_id > 0){ //and if you want to set that image as Post then use: update_post_meta($new_post,'_thumbnail_id',$attach_id); }
and you image will be uploaded and saved as post thumbnail.
-
Merci @Bainternet.J'avais dumal à lefaireinsérer la vignette,maispour une raison quelconque,lorsquej'ai remplacé '$new_post'par '$pid',ilinsère la vignette de l'articleThank you @Bainternet. I was struggling to get it to insert the thumbnail, but for some reason when i replaced '$new_post' with '$pid' it inserts the post thumbnail
- 0
- 2011-03-13
- Govnah Antwi-Boasiako
-
loolje suistellement stupide.Je viensjuste de comprendrepourquoije devais utiliser '$pid' j'avais cette ligne `$pid=wp_insert_post ($new_post);`lool i'm so stupid. I just clocked on why i had to use '$pid' i had this line `$pid = wp_insert_post($new_post);`
- 0
- 2011-03-13
- Govnah Antwi-Boasiako
-
Heureux que vous ayeztravaillé,et mieux c'est que vous avez compris.Glad you worked it out , and better is that you got the point.
- 0
- 2011-03-13
- Bainternet
-
Oui,mercibeaucouppour votre aide.Maintenant queje le comprends,ilesttemps d'ajouter unpeu d'ajax :)Yep, thank you very much for your help. Now that i understand it, its time to add some ajax :)
- 0
- 2011-03-13
- Govnah Antwi-Boasiako
-
@ Bainternet ♦mercipour votre réponse,c'est utile,mais comment lefairefonctionner avec l'imagemultiple.Je changemonentréeen
alors cen'est rienen train d'uploding.-aucune aide s'il vousplaît? @Bainternet♦ thanks for your answer it's helpfull, but how to make it work with the multiple image. I change my input to then it's not uploding anything. -any help please?- 0
- 2012-03-11
- Ehab Fawzy
-
Malheureusement,je n'ai qu'un seul compte dans Stackoverflow,je nepeux donc donner qu'un seul votepositif à cette question.Réponseparfaite.Unfortunately I have only one account in Stackoverflow so I can give Only one upvote to this Question. Perfect answer.
- 1
- 2015-09-27
- hemnath mouli
-
- 2011-03-29
Balisage HTML:
<p> <label for="custom-upload">Upload New Image:</label> <input type="file" tabindex="3" name="custom-upload" id="custom-upload" /> </p> <?php /*Retrieving the image*/ $attachment = get_post_meta($postid, 'custom_image'); if($attachment[0]!='') { echo wp_get_attachment_link($attachment[0], 'thumbnail', false, false); } ?>
Téléchargement de l'image:
<?php global $post; /*Global post object*/ $post_id = $post->ID; /*Geting current post id*/ $upload = $_FILES['upload']; /*Receive the uploaded image from form*/ add_custom_image($post_id, $upload); /*Call image uploader function*/ function add_custom_image($post_id, $upload) { $uploads = wp_upload_dir(); /*Get path of upload dir of wordpress*/ if (is_writable($uploads['path'])) /*Check if upload dir is writable*/ { if ((!empty($upload['tmp_name']))) /*Check if uploaded image is not empty*/ { if ($upload['tmp_name']) /*Check if image has been uploaded in temp directory*/ { $file=handle_image_upload($upload); /*Call our custom function to ACTUALLY upload the image*/ $attachment = array /*Create attachment for our post*/ ( 'post_mime_type' => $file['type'], /*Type of attachment*/ 'post_parent' => $post_id, /*Post id*/ ); $aid = wp_insert_attachment($attachment, $file['file'], $post_id); /*Insert post attachment and return the attachment id*/ $a = wp_generate_attachment_metadata($aid, $file['file'] ); /*Generate metadata for new attacment*/ $prev_img = get_post_meta($post_id, 'custom_image'); /*Get previously uploaded image*/ if(is_array($prev_img)) { if($prev_img[0] != '') /*If image exists*/ { wp_delete_attachment($prev_img[0]); /*Delete previous image*/ } } update_post_meta($post_id, 'custom_image', $aid); /*Save the attachment id in meta data*/ if ( !is_wp_error($aid) ) { wp_update_attachment_metadata($aid, wp_generate_attachment_metadata($aid, $file['file'] ) ); /*If there is no error, update the metadata of the newly uploaded image*/ } } } else { echo 'Please upload the image.'; } } } function handle_image_upload($upload) { global $post; if (file_is_displayable_image( $upload['tmp_name'] )) /*Check if image*/ { /*handle the uploaded file*/ $overrides = array('test_form' => false); $file=wp_handle_upload($upload, $overrides); } return $file; } ?>
HTML Markup:
<p> <label for="custom-upload">Upload New Image:</label> <input type="file" tabindex="3" name="custom-upload" id="custom-upload" /> </p> <?php /*Retrieving the image*/ $attachment = get_post_meta($postid, 'custom_image'); if($attachment[0]!='') { echo wp_get_attachment_link($attachment[0], 'thumbnail', false, false); } ?>
Uploading the image:
<?php global $post; /*Global post object*/ $post_id = $post->ID; /*Geting current post id*/ $upload = $_FILES['upload']; /*Receive the uploaded image from form*/ add_custom_image($post_id, $upload); /*Call image uploader function*/ function add_custom_image($post_id, $upload) { $uploads = wp_upload_dir(); /*Get path of upload dir of wordpress*/ if (is_writable($uploads['path'])) /*Check if upload dir is writable*/ { if ((!empty($upload['tmp_name']))) /*Check if uploaded image is not empty*/ { if ($upload['tmp_name']) /*Check if image has been uploaded in temp directory*/ { $file=handle_image_upload($upload); /*Call our custom function to ACTUALLY upload the image*/ $attachment = array /*Create attachment for our post*/ ( 'post_mime_type' => $file['type'], /*Type of attachment*/ 'post_parent' => $post_id, /*Post id*/ ); $aid = wp_insert_attachment($attachment, $file['file'], $post_id); /*Insert post attachment and return the attachment id*/ $a = wp_generate_attachment_metadata($aid, $file['file'] ); /*Generate metadata for new attacment*/ $prev_img = get_post_meta($post_id, 'custom_image'); /*Get previously uploaded image*/ if(is_array($prev_img)) { if($prev_img[0] != '') /*If image exists*/ { wp_delete_attachment($prev_img[0]); /*Delete previous image*/ } } update_post_meta($post_id, 'custom_image', $aid); /*Save the attachment id in meta data*/ if ( !is_wp_error($aid) ) { wp_update_attachment_metadata($aid, wp_generate_attachment_metadata($aid, $file['file'] ) ); /*If there is no error, update the metadata of the newly uploaded image*/ } } } else { echo 'Please upload the image.'; } } } function handle_image_upload($upload) { global $post; if (file_is_displayable_image( $upload['tmp_name'] )) /*Check if image*/ { /*handle the uploaded file*/ $overrides = array('test_form' => false); $file=wp_handle_upload($upload, $overrides); } return $file; } ?>
J'essaie defaire quelque chose de similaire à la question ci-dessus.J'essaie defaireen sorte que les utilisateurspublientet téléchargent desimages depuis lefront-end.J'ai déjàfait leformulaire depublicationet sonfonctionnement.
Je viens de suivreet d'essayer la réponsepubliéepar Robin I Knight upload-post-miniature-du-front-end .Malheureusement,je n'aipaspu lefairefonctionner.Y a-t-il quelque chose queje suis censé changer oumodifier?
Merci.