从前端上传帖子缩略图


Answers:


25

在ajax中上传文件有点棘手,因为无法使用浏览器的XMLHttpRequest对象上传文件,因此您需要使用某种Ajax上传插件,最简单的是JQuery Form Plugin,它使事情变得更加容易,并且包含在WordPress中。因此,要使用它,您需要使其入队:

add_action('wp_print_scripts','include_jquery_form_plugin');
function include_jquery_form_plugin(){
    if (is_page('12')){ // only add this on the page that allows the upload
        wp_enqueue_script( 'jquery' );
        wp_enqueue_script( 'jquery-form',array('jquery'),false,true ); 
    }
}

在该页面上,添加您的上传表单和JQuery以调用JQuery Form插件,例如:

<form id="thumbnail_upload" method="post" action="#" enctype="multipart/form-data" >
  <input type="file" name="thumbnail" id="thumbnail">
  <input type='hidden' value='<?php wp_create_nonce( 'upload_thumb' ); ?>' name='_nonce' />
  <input type="hidden" name="post_id" id="post_id" value="POSTID">
  <input type="hidden" name="action" id="action" value="my_upload_action">
  <input id="submit-ajax" name="submit-ajax" type="submit" value="upload">
<form>
<div id="output1"></div>
<script>
$(document).ready(function() { 
    var options = { 
        target:        '#output1',      // target element(s) to be updated with server response 
        beforeSubmit:  showRequest,     // pre-submit callback 
        success:       showResponse,    // post-submit callback 
        url:    ajaxurl                 // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php     
    }; 

    // bind form using 'ajaxForm' 
    $('#thumbnail_upload').ajaxForm(options); 
});
function showRequest(formData, jqForm, options) {
//do extra stuff before submit like disable the submit button
$('#output1').html('Sending...');
$('#submit-ajax').attr("disabled", "disabled");
}
function showResponse(responseText, statusText, xhr, $form)  {
//do extra stuff after submit
}
</script>

您必须使用实际的帖子ID更新POSTID。然后创建Ajax函数以接受文件上传并更新帖子缩略图

//hook the Ajax call
//for logged-in users
add_action('wp_ajax_my_upload_action', 'my_ajax_upload');
//for none logged-in users
add_action('wp_ajax_nopriv_my_upload_action', 'my_ajax_upload');

function my_ajax_upload(){
//simple Security check
    check_ajax_referer('upload_thumb');

//get POST data
    $post_id = $_POST['post_id'];

//require the needed files
    require_once(ABSPATH . "wp-admin" . '/includes/image.php');
    require_once(ABSPATH . "wp-admin" . '/includes/file.php');
    require_once(ABSPATH . "wp-admin" . '/includes/media.php');
//then loop over the files that were sent and store them using  media_handle_upload();
    if ($_FILES) {
        foreach ($_FILES as $file => $array) {
            if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
                echo "upload error : " . $_FILES[$file]['error'];
                die();
            }
            $attach_id = media_handle_upload( $file, $post_id );
        }   
    }
//and if you want to set that image as Post  then use:
  update_post_meta($post_id,'_thumbnail_id',$attach_id);
  echo "uploaded the new Thumbnail";
  die();
} 

希望这可以帮助


太好了 只是几个查询。也就是说,一切都需要去哪里。显然,表单放在相关页面上,这将是要使用的帖子ID。第一个添加操作是HEAD区域或functions.php的添加操作。最后的ajax块以// hook ajax调用开头。那去哪儿了。非常感谢。
罗宾一世骑士

第一个和最后一个代码段可以放置在您的function.php中的任何位置,第二个代码段需要放置在您希望在其上显示上传表单的页面上,您还可以将第二个代码段转换为简码以使事情变得更容易。
Bainternet 2011年

我没有得到的是表单如何知道使用my_ajax_upload()?那不应该包含在ajax调用中吗?我之所以这样问是因为我有一个循环的帖子,可以编辑,并且它们需要不同的功能来处理某些帖子。
Pollux Khafra 2012年

该表单知道使用my_ajax_upload,因为它的操作值已与(add_action(...)挂钩my_ajax_upload
Bainternet

WP最近是否有任何更改会影响此功能?由于某种原因,即使到JS回调中,param包含了我期望的所有内容,但$_POST我到时都没有数据。my_ajax_uploadshowRequestformData
drzaus
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.