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

时间:2011-03-13 作者:Govnah Antwi-Boasiako

我正在尝试做与上述问题类似的事情。我试图让用户从前端发布和上传图像。我已经完成了post表单及其工作。

我只是跟着,试着回答罗宾一世奈特的问题upload-post-thumbnail-from-the-front-end. 遗憾的是,我无法让它工作。有什么我想更改或编辑的吗?

非常感谢。

2 个回复
最合适的回答,由SO网友:Bainternet 整理而成

如果你在谈论我发布的答案here它只需在iframe中上传文件即可实现“类似Ajax”的提交。

现在,如果您已经有了一个处理post提交的表单,只需在表单中的某个位置添加上载文件字段输入:

<form ...
...
<input type="file" name="thumbnail" id="thumbnail">
...
...
</form>
确保您的表单enctype="multipart/form-data" 属性

然后在创建帖子后,在表单处理脚本中(假设您正在使用wp_insert_post();)在新var中保留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);
            }
您的图像将上载并保存为后期缩略图。

SO网友:Arvind07

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;
}
?>
结束

相关推荐

Posts wont expire

我在安排帖子自动过期(要么删除,要么起草)方面遇到了一些麻烦,我尝试过的每个插件都没有做任何事情,当它达到预定时间时,什么都没有发生,这让我觉得这可能是我一直忽视的一件简单的事情。。我想我可能对wp-cron有问题,但我似乎在通过wordPress设置未来的发布日期方面没有任何问题。我运行了最新版本的Wordpress,并设置了多个站点。所有插件都是目前可用的最新版本。有人有什么想法吗??我没有东西可以尝试了。。。提前感谢塔夫芝