提交帖子并从前端上传图片


Answers:


22

如果您在谈论我在此处发布的答案, 只需在iframe中上传文件即可实现“喜欢Ajax”提交。

现在,如果您已经有一个用于处理帖子提交的表单,则只需在表单中的某处添加上载文件字段输入:

<form ...
...
<input type="file" name="thumbnail" id="thumbnail">
...
...
</form>

确保您的表单具有enctype="multipart/form-data"属性。

然后在创建帖子后(假设您正在使用wp_insert_post();),在表单处理脚本中将帖子ID保留在新的变量中:

$new_post = wp_insert_post($post_array);

然后添加:

            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);
            }

您的图片将被上传并保存为帖子缩略图。


谢谢@Bainternet。我正在努力使其插入缩略图,但由于某些原因,当我将'$ new_post'替换为'$ pid'时,它会插入缩略图
Govnah Antwi-Boasiako 2011年

大声笑我是如此愚蠢。我只是想知道为什么我必须使用'$ pid'我有这行$pid = wp_insert_post($new_post);
Govnah Antwi-Boasiako 2011年

很高兴您能解决,更好的是您明白了。
Bainternet 2011年

是的,非常感谢您的帮助。现在我了解了,是时候添加一些ajax了:)
Govnah Antwi-Boasiako 2011年

1
不幸的是,我在Stackoverflow中只有一个帐户,因此我只能对此问题提供一个投票。完美的答案。
hemnath mouli 2015年

1

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);
  }

 ?>

上载图片:

<?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;
}
?>
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.