有人可以告诉我为什么这个功能没有将上传的文件移动到任何地方。
function handle_logo_upload($option){
if(!function_exists('wp_handle_upload'))
{
require_once(ABSPATH .'wp-admin/includes/file.php');
}
if(!empty($_FILES["site_logo_custom"])){
$theFile=$_FILES["site_logo_custom"];
$overrides=array('test_form'=>false);
$urls=wp_handle_upload($theFile,$overrides);
$temp=$urls["url"];
return $temp;
}
return $option;
}
我真的无法找到关于wp_handle_upload函数的太多内容。 Thankss !!!
答案 0 :(得分:1)
我认为您的表单格式有点像这样:
form action="" enctype="multipart/form-data" method="post"> //action is current post
<input type="file" name="file">
<input type="submit" name="submit">
</form>
使用wp_handle_upload()将文件上传到wordpress上传文件夹;您可以在代码下方使用的功能....
function handle_logo_upload($file){
require_once(ABSPATH.'wp-admin/includes/file.php');
$uploadedfile = $file;
$movefile = wp_handle_upload($uploadedfile, array('test_form' => false));
if ( $movefile ){
echo $movefile['url'];
//or return
return $movefile['url'];
}
}
if (isset($_POST['submit'])) {
handle_logo_upload($_FILES['file']);
}
答案 1 :(得分:0)
猜测你的功能是完美的。
function handle_logo_upload($option){
if(!function_exists('wp_handle_upload'))
{
require_once(ABSPATH .'wp-admin/includes/file.php');
}
//you are using empty version make sure your php version is higher than 5.2
if(!empty($_FILES["site_logo_custom"])){
$move_logo = wp_handle_upload( $_FILES["site_logo_custom"], array('test_form' => false) );
if ( $move_logo && !isset($move_logo['error']) ) {
$wp_upload_dir = wp_upload_dir();
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename($move_logo['file']),
'post_mime_type' => $move_logo['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename($move_logo['file']) ),
'post_content' => '',
'post_status' => 'inherit'
);
$logo_attach_id = wp_insert_attachment($attachment, $move_logo['file']);
$image_attributes = wp_get_attachment_image_src( $logo_attach_id );
if ( $image_attributes ) {
return $image_attributes[0];
}
else
{
return $option;
}
}else{
return $option;
}
}else{
return $option;
}
}
请阅读我在代码中写的评论