I have been trying to use the unzip_file() function. It says undefined so I looked into it and the WP_Filesystem() must be called and set up. So easy, right?

require_once(ABSPATH .'/wp-admin/includes/file.php');

WP_Filesystem();

unzip_file( $zip, $dest  );

Even this shows as undefined, and I don't see any documentation on it. I am trying to use the unzip feature to unpack a separate plugin during theme install.

  • You need to debug. Use breakpoints or turn WP_DEBUG on. If the returned var from unzip_file() is undefined, then the function stop working. – Ralf912 May 11 '13 at 3:37
  • Apparently you can't use this outside of admin? This is not heavily documented so I'm quite in the dark. – user1915665 May 11 '13 at 4:01
  • Huh? What about the admin? WP_DEBUG works in frontend and backend. And unzip_file() also. Please post your code. – Ralf912 May 11 '13 at 4:11
  • Code is now above in my post. At first I called "unzip_file" which did nothing. Then I did WP_Filesystem();...which you know was undefined as well. The script was initiated after a theme activation hook - so for some reason these functions are invisible in that context. – user1915665 May 11 '13 at 4:44
  • You need to use $creds from request_filesystem_credentials to #Initializing_WP_Filesystem_Base codex.wordpress.org/… – jgraup May 10 '16 at 1:28
require_once(ABSPATH .'/wp-admin/includes/file.php');

global $wp_filesystem;
if ( ! $filesystem ) {
  WP_Filesystem();
    if ( ! $wp_filesystem )
      nuke_the_world_because_wpfs_cannot_be_initialized_in_case_of_missing_arguments('!');
}

$result = unzip_file( $zip, $dest );
if ( is_wp_error( $result ) )
   nuke_the_world();

Maybe some error handling make it easier.

If you can't get WordPress to unzip the file, PHP has its own zip file implementation (documentation can be found at http://php.net/manual/en/book.zip.php). Simple examples can be found at http://www.php.net/manual/en/zip.examples.php.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.