因此,我正在构建我自己的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
要运行这段代码,我需要更改到处理程序函数中的什么?
干杯!
发布于 2015-09-14 07:23:07
您可以尝试这个例子,它的例子,您应该更改您自己的变量名和所有这些:
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.";
}
}发布于 2018-10-29 07:14:55
在enctype="multipart/form-data"标记中添加<form>。就像<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;
}完成了!:)
https://stackoverflow.com/questions/32474491
复制相似问题