I got it to work without errors by using it inside add_attachment
hook below, but now the problem is it runs on every media upload.
function unzip_icon_fonts( $post_ID ){
$file_path = get_attached_file( $post_ID );
require_once(ABSPATH . 'wp-admin/includes/file.php');
WP_Filesystem();
$destination = wp_upload_dir();
$destination_path = $destination['path'];
$unzipfile = unzip_file($file_path, $destination_path);
}
add_action( 'add_attachment', 'unzip_icon_fonts' );
I tried restricting it to a specific Customizer file upload below, but then it doesn’t work at all.
function unzip_icon_fonts( $post_ID ){
// If attachment ID equals Customizer file upload ID added with WP_Customize_Media_Control
if ( $post_ID == get_theme_mod('icon_fonts', '') ) {
$file_path = get_attached_file( $post_ID );
require_once(ABSPATH . 'wp-admin/includes/file.php');
WP_Filesystem();
$destination = wp_upload_dir();
$destination_path = $destination['path'];
$unzipfile = unzip_file($file_path, $destination_path);
}
}
add_action( 'add_attachment', 'unzip_icon_fonts' );
Am I missing something?