acf_upload_file() │ ACF 5.0.9
acf_upload_file
This function will uploade a $_FILE
Хуки из функции
Возвращает
$id. (int) new attachment ID
Использование
acf_upload_file( $uploaded_file );
- $uploaded_file(обязательный)
- .
Список изменений
| С версии 5.0.9 | Введена. |
Код acf_upload_file() acf upload file ACF 6.8.11
function acf_upload_file( $uploaded_file ) {
// required
// require_once( ABSPATH . "/wp-load.php" ); // WP should already be loaded
require_once ABSPATH . '/wp-admin/includes/media.php'; // video functions
require_once ABSPATH . '/wp-admin/includes/file.php';
require_once ABSPATH . '/wp-admin/includes/image.php';
// required for wp_handle_upload() to upload the file
$upload_overrides = array( 'test_form' => false );
// Restrict uploads to image MIME types for image and gallery fields.
// phpcs:disable WordPress.Security.NonceVerification.Missing -- Field key is used for validation context only.
if ( ! empty( $_POST['_acfuploader'] ) ) {
$field = acf_get_field( sanitize_text_field( wp_unslash( $_POST['_acfuploader'] ) ) );
if ( acf_is_image_field( $field ) ) {
$upload_overrides['mimes'] = acf_get_image_mime_types( $field );
}
}
// phpcs:enable WordPress.Security.NonceVerification.Missing
// upload
$file = wp_handle_upload( $uploaded_file, $upload_overrides );
// bail early if upload failed
if ( isset( $file['error'] ) ) {
return $file['error'];
}
// vars
$url = $file['url'];
$type = $file['type'];
$file = $file['file'];
$filename = basename( $file );
// Reject any PDF without a %PDF- header.
if ( 'application/pdf' === $type ) {
$head = file_get_contents( $file, false, null, 0, 1024 );
if ( false === $head || 0 !== strpos( ltrim( $head, "\x00..\x20" ), '%PDF-' ) ) {
wp_delete_file( $file );
return __( 'Sorry, this file could not be uploaded.', 'acf' );
}
}
// Construct the object array
$object = array(
'post_title' => $filename,
'post_mime_type' => $type,
'guid' => $url,
);
// Save the data
$id = wp_insert_attachment( $object, $file );
/**
* Filters whether a file uploaded through a basic uploader gets preview images.
*
* Applies to File, Image and Gallery fields using the basic uploader, such as in
* front-end forms. When false, WordPress won't render PDF previews or save cover
* art embedded in audio and video files while generating the attachment metadata.
*
* @since 6.8.11
*
* @param boolean $generate_previews Whether to generate preview images. Default true only for users who can upload files.
* @param string $file Path to the uploaded file.
* @param integer $id Attachment ID.
*/
$skip_previews = ! apply_filters( 'acf/upload/generate_previews', current_user_can( 'upload_files' ), $file, $id );
if ( $skip_previews ) {
// Dedicated callbacks, so removing them can't also remove a matching filter added by a site.
$no_preview_sizes = static function () {
return array();
};
$no_cover_art = static function ( $metadata ) {
if ( is_array( $metadata ) ) {
unset( $metadata['image'] );
}
return $metadata;
};
add_filter( 'fallback_intermediate_image_sizes', $no_preview_sizes, PHP_INT_MAX );
add_filter( 'wp_read_audio_metadata', $no_cover_art, PHP_INT_MAX );
add_filter( 'wp_read_video_metadata', $no_cover_art, PHP_INT_MAX );
}
try {
// Add the meta-data
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
} finally {
// Always detach the temporary filters so they can't affect anything else in this request.
if ( $skip_previews ) {
remove_filter( 'fallback_intermediate_image_sizes', $no_preview_sizes, PHP_INT_MAX );
remove_filter( 'wp_read_audio_metadata', $no_cover_art, PHP_INT_MAX );
remove_filter( 'wp_read_video_metadata', $no_cover_art, PHP_INT_MAX );
}
}
/** This action is documented in wp-admin/custom-header.php */
do_action( 'wp_create_file_in_uploads', $file, $id ); // For replication
// return new ID
return $id;
}