因此,我有一个包含两个图像字段()的表单,并且创建了一个函数,该函数可以非常好整齐地上传文件。所有文件(仅允许图像)显示在媒体库中。
但是现在我需要它们在媒体库中不可见。
这是我使用的功能,我什至有wp_insert_attachment,现在对其进行了注释,希望它足够了。
图像仍显示在媒体中。
function insertImage ($file){
$wordpress_upload_dir = wp_upload_dir();
$i = 1; // number of tries when the file with the same name is already exists
$upload_dir_custom = $wordpress_upload_dir['basedir']."/custom-folder";
if (!file_exists($upload_dir_custom)) {
mkdir( $upload_dir_custom,0755,true) ;
}
$new_file_path = $upload_dir_custom;
$new_file_mime = mime_content_type( $file['tmp_name'] );
if( empty( $file ) )
die( 'File is not selected.' );
if( $file['error'] )
die( $file['error'] );
if( $file['size'] > wp_max_upload_size() )
die( 'Image is too large!.' );
if( !in_array( $new_file_mime, get_allowed_mime_types() ) )
die( 'WordPress doesn\'t allow this type of uploads.' );
while( file_exists( $new_file_path ) ) {
$i++;
if($i>1){
$new_file_path = $wordpress_upload_dir['basedir'] . '/sponsor-info/' . $i . '_' .$file['name'];
}else{
$new_file_path = $wordpress_upload_dir['basedir'] . '/sponsor-info/'.$file['name'];
}
}
if( move_uploaded_file( $file['tmp_name'], $new_file_path ) ) {
$image_url = $wordpress_upload_dir['url'] . '/sponsor-info/' . basename( $new_file_path);
return $image_url;
}
}