我设法使用以下代码上传我的媒体。不幸的是,媒体在与前端表单发布日期(/ 2014/09 /)相同的月份保存。这是上传用户个人资料图片,而不是附加到任何帖子。希望有人可以帮忙,因为我无法在网上找到任何资源。谢谢!
// UPLOAD USER IMAGE
function upload_user_image($user, $nonce_name, $input_name, $field_name ) {
if (
isset( $_POST[$nonce_name] )
&& wp_verify_nonce( $_POST[$nonce_name], $input_name )
) {
$arr_file_type = wp_check_filetype(basename($_FILES[$input_name]['name']));
$uploaded_file_type = $arr_file_type['type'];
$allowed_file_types = array('image/jpg','image/jpeg','image/gif','image/png');
if(in_array($uploaded_file_type, $allowed_file_types)) {
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
$attachment_id = media_handle_upload( $input_name, 0 );
if ( is_wp_error( $attachment_id ) ) {
$upload_feedback = 'There was a problem with your upload.';
} else {
$upload_feedback = false;
}
update_user_meta( $user, $field_name, $attachment_id );
} else {
$upload_feedback = 'Please upload only image files (jpg, gif or png).';
}
} else {
$upload_feedback = 'Security check failed';
}
}