2012-02-06 95 views
1

我需要一些幫助! :)前端媒體上傳WordPress的

我想要做的是讓用戶從前端創建一個帖子。預覽窗口顯示發佈後這篇文章的外觀。這我已經完成了。現在我試圖讓用戶直接將圖片上傳到媒體庫中。這需要上傳到只要用戶選擇的文件與後續的代碼發生:

<form method="post" action="#" enctype="multipart/form-data" > 
     <input type="file" name="featured_image"> 
</form> 

我不希望任何頁面加載,有沒有辦法來獲取文件,而無需使用一個提交按鈕? 還有我該如何處理實際的文件上傳,我的谷歌技能發現這個功能:

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

不過這個功能掛鉤上傳到帖子,我只是希望它直接進入庫,而無需連接到帖子。另外我假設$ file_handler是表單的名稱?我還假設$ _FILES不存在,除非用戶點擊了一個提交按鈕。

我很快就在這裏,說出我的屁股。所以任何幫助都會受到歡迎。任何WP-gurus可用? :D

+0

因此,使用上面的代碼中,我得到它的在頁面r上工作最新,但我怎麼用ajax做到這一點?我需要上傳文件而不必刷新。發送一個$ .ajax請求到一個處理上述函數的php文件會產生錯誤,因爲它不能讀取$ _FILES數組,因爲它永遠不會被聲明。 – ninja 2012-02-07 09:41:57

回答

1

好吧,我正在前進。我現在使用這個jquery插件進行圖片上傳 - > http://lagoscript.org/jquery/upload

這是非常輕量級的,很好用。將圖像上傳到我的電腦而不刷新。它將文件保存在我的服務器上的臨時目錄中,我打算現在要做的是以某種方式將此映像注入媒體庫(然後將其從臨時文件夾中刪除),但這似乎比應該更難。

有沒有什麼辦法可以用php-code將圖片添加到媒體庫,只使用圖片url? 我似乎無法找到一個,但由於WP已經在管理區域中具有此功能,所以必須有一種方法。

最簡單的方法似乎是剛剛創建,存儲該訊息的形象自定義字段,但是我真的希望能夠使用WPS功能縮略圖,不僅爲循環,但對於調整等

任何指針?

+0

鏈接已關閉 – 2014-01-10 18:47:47

4

我有同樣的問題。 WordPress的文檔可幫助: http://codex.wordpress.org/Function_Reference/wp_handle_upload http://codex.wordpress.org/Function_Reference/wp_insert_attachment

UPDATE:$關鍵是類型= 「文件」

我剛添加的GUID值的名稱,因爲它似乎沒有添加(也許有更好的方法,然後包括管理員。PHP:

 require_once(ABSPATH . 'wp-admin/includes/admin.php'); 
     // step one upload file to server 
     $file_return = wp_handle_upload($_FILES[$key], array('test_form' => false)); 

     if(isset($file_return['error']) || isset($file_return['upload_error_handler'])) { 
      echo 'not working again :('; 
     } 
     else { 
      /** 
      * See http://codex.wordpress.org/Function_Reference/wp_insert_attachment 
      * 
      */ 
      $filename = $file_return['file']; 

      $attachment = array(
       'post_mime_type' => $file_return['type'], 
       'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)), 
       'post_content' => '', 
       'post_status' => 'inherit', 
       'guid' => $file_return['url'] 
      ); 
      $attach_id = wp_insert_attachment($attachment, $file_return['url']); 
      // you must first include the image.php file 
      // for the function wp_generate_attachment_metadata() to work 
      require_once(ABSPATH . 'wp-admin/includes/image.php'); 
      $attach_data = wp_generate_attachment_metadata($attach_id, $filename); 
      wp_update_attachment_metadata($attach_id, $attach_data); 

     } 


    } 
-1

我使用這個代碼的工作是好的

的JavaScript

<script src="<?php bloginfo('template_url'); ?>/image_file/jquery.ui.widget.js"></script> 
<script src="<?php bloginfo('template_url'); ?>/image_file/jquery.iframe-transport.js"></script> 
<script src="<?php bloginfo('template_url'); ?>/image_file/jquery.fileupload.js"></script> 
<script src="<?php bloginfo('template_url'); ?>/image_file/jquery.fileupload-validate.js"></script> 

<script> 

$(function() { 
    'use strict'; 

    var url = '<?php bloginfo('template_url'); ?>/upload.php'; 
    $('#fileupload').fileupload({ 


     add: function(e, data) { 
       var uploadErrors = []; 

       if(data.originalFiles[0]['size'] > 2000000) { 
        uploadErrors.push('Filesize is too big'); 
       } 
       if(uploadErrors.length > 0) { 

        $("#errormessage").show(); 
        $("#errormessage").html(uploadErrors.join("\n")); 

       } else { 
        data.submit(); 
        $("#errormessage").hide(); 
       } 
     }, 


     url: url, 
     dataType: 'json', 

     done: function (e, data) { 


      if(data._response.result.response=="errortype"){ 
       $("#errormessage").show(); 
       $("#errormessage").html("Not an accepted file type "); 
       $("#progress").hide(); 
      } 
      else 
      { 
        $("#progress").show(); 
        var url=data._response.result.upload_info.url; 
       if(url=='') 
        { 

        } 
        $("#file_url").val(url); 


        } 



     }, 
     progressall: function (e, data) { 

      var progress = parseInt(data.loaded/data.total * 100, 10); 
      $('#progress .progress-bar').html(progress + '%'); 
      $('#progress .progress-bar').css(
       'width', 
       progress + '%' 
      ); 
     } 
    }).prop('disabled', !$.support.fileInput) 
     .parent().addClass($.support.fileInput ? undefined : 'disabled'); 
}); 
</script> 

HTML

<

span class="input__label-content input__label-content--akira" >UPLOAD A PHOTO OF YOUR RECEIPT**</span> 
       <span id="fileupload"> <input type="file" name="upload_file" multiple></span> 

    </span> 

    <input type="hidden" name="file_url" value="" id="file_url" /> 

upload.php 

<?php 

    require("../../../wp-load.php"); 
    global $wpdb; 
    if (! function_exists('wp_handle_upload')) require_once(ABSPATH . 'wp-admin/includes/file.php'); 
$uploadedfiles = $_FILES['upload_file']; 

if(strtolower($uploadedfiles['type'])=='image/jpeg' || strtolower($uploadedfiles['type'])=='image/gif' || strtolower($uploadedfiles['type'])=='image/png' || strtolower($uploadedfiles['type'])=='image/pjpeg' || strtolower($uploadedfiles['type'])=='image/x-png'){ 
// 

$upload_overrides = array('test_form' => false); 
$results=array(); 


    if ($uploadedfiles['name']) { 
    $file = array(
     'name'  => $uploadedfiles['name'], 
     'type'  => $uploadedfiles['type'], 
     'tmp_name' => $uploadedfiles['tmp_name'], 
     'error' => $uploadedfiles['error'], 
     'size'  => $uploadedfiles['size'] 
    ); 


    $movefile = wp_handle_upload($file, $upload_overrides); 

    if ($movefile) { 

     $data=array('result'=>'success','upload_info'=>$movefile); 

     $filename = $movefile['upload_file']['file']; 

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



     $wp_upload_dir = wp_upload_dir(); 


     $attachment = array(
     'guid'   => $wp_upload_dir['url'] . '/' . basename($filename), 
     'post_mime_type' => $filetype['type'], 
     'post_title'  => preg_replace('/\.[^.]+$/', '', basename($filename)), 
     'post_content' => '', 
     'post_status' => 'inherit' 
    ); 

     $attach_id = wp_insert_attachment($attachment, $filename, $id); 


     require_once(ABSPATH . 'wp-admin/includes/image.php'); 


     $attach_data = wp_generate_attachment_metadata($attach_id, $filename); 
     wp_update_attachment_metadata($attach_id, $attach_data); 



    } else { 
     $data=array('result'=>'error','upload_info'=>null); 
    } 

    $data['request']=$_FILES; 

    } 
} 
else 
{ 
    $data=array('response'=>'errortype','upload_info'=>null); 
} 

    echo json_encode($data); 


?> 
相關問題