如果我没说错的话,我有一个很好的解决方案:你想在你的网站上为访问者显示一个上传框,让他能够将图像保存到wordpress库中,并将它们标记为useruploads之类的东西。
第一步是在帖子模板上创建表单:
<form action=\'upload.php?pid=$post->ID\' method=\'post\' enctype=\'multipart/form-data\'>
<input type=\'file\' name=\'myfile[]\' multiple class=\'realfile\'>
<div class=\'fakefile\'>
<input class=\'fakefile fakefileinput\'/>
<img src=\'" . get_bloginfo( \'template_url\' ) . "/img/find.png\' alt=\'search\' />
</div><br>
<span class=\'percent\'></span><br>
<input type=\'submit\' value=\'upload\'>
</form>
您可以看到我实现的一些技巧,fakefile和realfile用于设计框,使其在每个浏览器中看起来都一样,设置fakefile的样式,并隐藏真实的文件。你可以跳过这部分。接下来是javascript(我想您已经在站点上使用了jQuery),在document ready函数中:
var percent = $(\'.percent\');
$(\'form\').ajaxForm({
beforeSend: function() {
var percentVal = \'0%\';
percent.html(percentVal);
},
uploadProgress: function(event, position, total, percentComplete) {
var percentVal = percentComplete + \'%\';
percent.html(percentVal);
},
complete: function(xhr) {
var responseJson = eval(\'(\' + xhr.responseText + \')\');
if( responseJson.message != null )
alert(responseJson.message);
if ( responseJson.ids != null ) {
alert( \'Upload successful\' );
$(\'.fakefileinput\').val(\'\');
$(\'.realfile\').val(\'\');
percent.html(\'\');
}
}
});
这一部分处理完整的表单-包含上载的百分比计数。确保包括
<div class="percent"></div>
.
最后要做的是您的uploadhandler。我在根目录中有一个叫做upload的。php,正如您在我的表单中看到的那样,但是每个文件夹都应该工作,只要您包含wp负载。php。
include_once( \'wp-load.php\' ); // to have the wordpress functions available
$uploaddir = wp_upload_dir(); // get the directory to store the images
if ( isset( $_FILES[\'myfile\'][\'name\'][\'0\'] ) ) { // check if there are files
$files = $_FILES[\'myfile\'];
$number = count( $files[\'name\'] );
for ( $_i = 0; $_i < $number; $_i++ ) { // loop through the uploaded images
if ( "image/jpeg" != $files[\'type\'][$_i] && "image/pjpeg" != $files[\'type\'][$_i] ) {
// just allow jpeg, be sure to include pjpeg as the IE sends pjpeg instead of jpeg.
$_message.= \'Picture is no Jpeg: \' . $files[\'type\'][$_i];
} else {
$uploadfile = $uploaddir[\'path\'] . \'/\' . $_GET[\'pid\'] . \'-\' . basename($files[\'name\'][$_i]); // name and directory of the file. for ordering purposes in the file system, i include the postid in front of the filename.
move_uploaded_file($files[\'tmp_name\'][$_i], $uploadfile); //move the file from the server upload directory to the wordpress upload directory
$filename = basename($uploadfile); // get the filename
$wp_filetype = wp_check_filetype(basename($filename), null ); //wordpress handler for filetypes, although we handle just jpeg here.
// data of the new attachment. you can add any values here, for example an excerpt or whatever
$attachment = array(
\'post_mime_type\' => $wp_filetype[\'type\'],
\'post_title\' => preg_replace(\'/\\.[^.]+$/\', \'\', $filename),
\'post_content\' => \'\',
\'post_status\' => \'inherit\'
);
$attach_id = wp_insert_attachment( $attachment, $uploadfile ); // insert the attachment into the wordpress database - the file HAS to be in the uploaddir! the function returns the new id.
// 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\');
$image = get_post( $attach_id ); // load the data of the new image.
$fullsizepath = get_attached_file( $image->ID ); // get the filepath of the image in the wordpress upload directory
$attach_data = wp_generate_attachment_metadata( $attach_id, $fullsizepath ); // generate the thumbnails and the rest of the metadata
wp_update_attachment_metadata( $attach_id, $attach_data ); // ... and update the image
$mypost = array(
\'ID\' => $image->ID,
\'post_parent\' => $_GET[\'pid\']
);
wp_update_post( $mypost ); // now set the post parent to the submitted post_id, so you have the connection between the uploaded files and the corresponding posts
wp_set_object_terms( $image->ID, \'userupload\', \'category\' ); // if you want to, you can also assign a category \'userupload\' to the uploaded images - you can distinguish between your own and the ones from the users.
//$_message.= var_dump( $attach_data ); - debugging
$_id[$_i]= $attach_id; // save all the new ids into an array to return, if you want to do anything special with them afterwards.
}
}
} else {
$_message.= "No Images sent."; // Errorhandler
}
$return = json_encode( array(
\'ukid\' => $_GET[\'uid\'],
\'message\' => $_message,
\'ids\' => $_id
) );
echo( $return ); // return the json, explaining what happenend.
die();
应该是这样!我对代码做了一点修改,后来没有对其进行测试,但它应该可以正常工作:)请确保包含某种垃圾邮件检测,确保您的服务器可以处理图像处理中的所有工作:)如果您对此方法有任何其他问题,请毫不犹豫地询问:)