所以我正在建立自己的WordPress主题,并且我添加了一个设置,可以更改网站徽标。
这是注册处理上传功能的部分:
add_settings_field("logo", "Logo", "logo_display", "theme-options", "section");
register_setting("section", "logo", "handle_logo_upload");
这是应该处理给定文件的处理函数:
function handle_logo_upload()
{
if ( ! function_exists( 'wp_handle_upload' ) ) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
}
$uploadedfile = $_FILES['file'];
$upload_overrides = array( 'test_form' => false );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
if ( $movefile && !isset( $movefile['error'] ) ) {
echo "File is valid, and was successfully uploaded.\n";
var_dump( $movefile);
} else {
/**
* Error generated by _wp_handle_upload()
* @see _wp_handle_upload() in wp-admin/includes/file.php
*/
echo $movefile['error'];
}
return 'stringaz';
}
这就是主题面板的样子:http://pasteboard.co/uwC3Qmi.png
在这里你可以看到处理程序无法正常工作,因为一个名为' stringaz'正在呈现徽标应显示的位置:http://pasteboard.co/vsnqkay.png
我需要更改为处理程序函数才能运行此代码?
喝彩!
答案 0 :(得分:0)
你可以尝试这个,例如,你应该改变你自己的变量名称和所有:
if(!empty($_FILES['user_image']["name"]))
{
if( $_FILES['user_image']['type'] == "image/jpeg" || $_FILES['user_image']['type'] == "image/png" || $_FILES['user_image']['type'] == "image/jpg")
{
$wp_upload_dir = wp_upload_dir();
$target_file = $wp_upload_dir['path']."/".$_FILES['user_image']["name"];
$target_file1 = $wp_upload_dir['url']."/".$_FILES['user_image']["name"];
if ( move_uploaded_file($_FILES['user_image']["tmp_name"], $target_file) )
update_user_meta($current_user->ID, 'cupp_upload_meta', $target_file1 );
else
$errmsg = "Sorry, there was an error uploading your file.";
}
else
{
$errmsg = "Image file have not valid extension.";
}
}
答案 1 :(得分:0)
在 <form>
标签中添加 enctype =“ multipart / form-data” 。
像 <form method="post" action="options.php" enctype="multipart/form-data">
function handle_logo_upload()
{
if(!function_exists('wp_handle_upload')){
require_once(ABSPATH.'wp-admin/includes/file.php');
}
if(!empty($_FILES["logo"]["tmp_name"]))
{
$urls = wp_handle_upload($_FILES["logo"], array('test_form' => FALSE));
$temp = $urls["url"];
return $temp;
}
return $option;
}
完成! :)