请帮助我。
这是代码...。效果很好,但是在媒体库上->它们显示“上传到”为“未附加”
add_action( 'wp_ajax_OWN_PROP_attachment_upload', 'OWN_PROP_attachment_upload' ); // only for logged in user
add_action( 'wp_ajax_nopriv_OWN_PROP_attachment_upload', 'OWN_PROP_attachment_upload' );
if( !function_exists( 'OWN_PROP_attachment_upload' ) ) {
function OWN_PROP_attachment_upload( ) {
// Check security Nonce
$verify_nonce = $_REQUEST['verify_nonce'];
if ( ! wp_verify_nonce( $verify_nonce, 'verify_gallery_nonce' ) ) {
echo json_encode( array( 'success' => false , 'reason' => 'Invalid nonce!' ) );
die;
}
$submitted_file = $_FILES['PROP_attachment_file'];
$uploaded_image = wp_handle_upload( $submitted_file, array( 'test_form' => false ) );
if ( isset( $uploaded_image['file'] ) ) {
$file_name = basename( $submitted_file['name'] );
$file_type = wp_check_filetype( $uploaded_image['file'] );
// Prepare an array of post data for the attachment.
$attachment_details = array(
'guid' => $uploaded_image['url'],
'post_mime_type' => $file_type['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file_name ) ),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment_details, $uploaded_image['file'] );
$attach_data = wp_generate_attachment_metadata( $attach_id, $uploaded_image['file'] );
wp_update_attachment_metadata( $attach_id, $attach_data );
$thumbnail_url = wp_get_attachment_image_src( $attach_id, 'OWN-image350_350' );
//$fullimage_url = wp_get_attachment_image_src( $attach_id, 'full' );
$attachment_title = get_the_title($attach_id);
$fullimage_url = wp_get_attachment_url( $attach_id );
$ajax_response = array(
'success' => true,
'url' => $thumbnail_url[0],
'attachment_id' => $attach_id,
'full_image' => $fullimage_url,
'attach_title' => $attachment_title,
);
echo json_encode( $ajax_response );
die;
} else {
$ajax_response = array( 'success' => false, 'reason' => 'File upload failed!' );
echo json_encode( $ajax_response );
die;
}
}
}
我不明白...从前端上传图像后,一切正常。
在媒体库上,他们显示谁上载但“上载到”的用户名是“未附加”。