Question

Conception

Apples d'or créé ( pour autant que je sache) la fonction des médias que beaucoup de gens sur WP.A et ailleurs semblent recommander. Mais tous les Q & R je peux trouver sur la création beaucoup StackExchange un tout nouveau poste.

Quelle est la meilleure façon d'obtenir ce (ou un autre extrait) pour télécharger des médias de la fin de l'avant, l'attacher au poste actuel, générer les vignettes appropriées, puis rafraîchir la page montrant la nouvelle image dans la galerie [ ]?

Voici le code Than: Dans le fichier de fonctions ...

function insert_attachment($file_handler,$post_id,$setthumb='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 ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);
  return $attach_id;

En-tête modèle ...

// set $post_id to the id of the post you want to attach
// these uploads to (or 'null' to just handle the uploads
// without attaching to a post)

if ($_FILES) {
  foreach ($_FILES as $file => $array) {
    $newupload = insert_attachment($file,$post_id);
    // $newupload returns the attachment id of the file that
    // was just uploaded. Do whatever you want with that now.
  }
}

Télécharger le formulaire utilisé ...

<form method="post" action="#" enctype="multipart/form-data" >
  <input type="file" name="an_uploaded_attachment">
  <input type="file" name="another_uploaded_attachment">
  <input type="file" name="yet_another_uploaded_attachment">
  <input type="submit">
<form>
Était-ce utile?

La solution

a répondu à cette question un ancien poste qui ne montrait pas dans ma diligence de recherche. Alors qu'il était en fait à la recherche de quelque chose d'autre son code d'exemple fonctionnait très bien.

Voici le script:

<?php $post_id = $post->ID;
if ( isset( $_POST['html-upload'] ) && !empty( $_FILES ) ) {
    require_once(ABSPATH . 'wp-admin/includes/admin.php');
    $id = media_handle_upload('async-upload', $post_id); //post id of Client Files page
    unset($_FILES);
    if ( is_wp_error($id) ) {
        $errors['upload_error'] = $id;
        $id = false;
    }

    if ($errors) {
        echo "<p>There was an error uploading your file.</p>";
    } else {
        echo "<p>Your file has been uploaded.</p>";
    }
}

?>
    <form id="file-form" enctype="multipart/form-data" action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="POST">

    <p id="async-upload-wrap"><label for="async-upload">upload</label>
    <input type="file" id="async-upload" name="async-upload"> <input type="submit" value="Upload" name="html-upload"></p>

    <p><input type="hidden" name="post_id" id="post_id" value="<?php echo $post_id ?>" />
    <?php wp_nonce_field('client-file-upload'); ?>
    <input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>" /></p>

    <p><input type="submit" value="Save all changes" name="save" style="display: none;"></p>
    </form>

Autres conseils

Dans votre changement d'en-tête $post_id à $post->ID.

// set $post_id to the id of the post you want to attach
// these uploads to (or 'null' to just handle the uploads
// without attaching to a post)

if ($_FILES) {
  foreach ($_FILES as $file => $array) {
    $newupload = insert_attachment($file,$post->ID);
    // $newupload returns the attachment id of the file that
    // was just uploaded. Do whatever you want with that now.
  }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange
scroll top