An error occurred while updating Lazy Blocks Pro: Could not create directory
If you know that your Lazy Blocks Pro license is active, and when you try to update Lazy Blocks Pro using the standard WordPress plugin update method but you’re met with an error that states:
An error occurred while updating Lazy Blocks Pro: Could not create directory. lazy-blocks-pro/assets
There is a fix available! For some background on the issue: Lazy Blocks’s update process uses signed URLs (to make the update more secure) which involves generation of a long string that’s used as the filename during the update process.
There is a limitation of the Windows operating system that prevents a file path from extending beyond a certain number of characters (250 or so) and Lazy Blocks’s signed update URLs can exceed this limit. This is what causes the “could not create directory” error message to appear.
It is possible to work around this issue, but it involves installing a Must Use Plugin. Must Use Plugins are different than Plugins in that they’re loaded first and they also require a manual installation.
This process needs to happen only once, but once it does your Lazy Blocks Pro updates should operate as expected.
You will need to create a new file within your /wp-content/mu-plugins
folder. If this folder doesn’t exist you must create it.
You can name the new file lazy-blocks-pro-windows-compat.php
and save the following contents in that file:
<?php
/**
* Plugin Name: Lazy Blocks Pro Windows Server Compatibility
* Plugin URI: https://www.lazyblocks.com/docs/troubleshooting/an-error-occurred-while-updating-lazy-blocks-pro-could-not-create-directory/
* Description: Resolves an issue on Windows servers that prevents Lazy Blocks Pro from automatically updating.
*/
/**
* @link https://gist.github.com/spivurno/d7a93ab4920c4fa88bad0e5177b45ba1
* Easy Digital Downloads provides packages URLs that look something like this:
*
* http://mysite.com/edd-sl/package_download/MTQ4NjA1NTA0NjphMDA5MTkzZjQ0NGRiNmVmMzczY2JhNTFiZWIxMWZiYzo0NzM3MzphM2Q5ZDA3NDQwMjZjZDFmOWVhYTBiNzBjMjVlZjI0YjpodHRwQC8vbXVzaWNmZXN0aXZhbC5zY2hvb2wubno
*
* This generates a temporary filename for the update process that look something like this:
*
* MTQ4NjA1NTA0NjphMDA5MTkzZjQ0NGRiNmVmMzczY2JhNTFiZWIxMWZiYzo0NzM3MzphM2Q5ZDA3NDQwMjZjZDFmOWVhYTBiNzBjMjVlZjI0YjpodHRwQC8vbXVzaWNmZXN0aXZhbC5zY2hvb2wubno.tmp
*
* Some Windows servers choke on filenames of this size. As a result, the update wil fail and WordPress
* will give you a "Could not copy file." error.
*
* This solution (which is a little weird) will check if the package URL that is about to be downloaded
* by WordPress is an EDD package URL. If so, it will truncate the temporary filename to 50 characters.
*/
// initiate a fix for Windows servers where the GP package file name is too long and prevents installs/updates from processing
add_filter( 'upgrader_pre_download', 'mu_lzb_pro_maybe_shorten_edd_filename', 10, 4 );
/**
* Check if the URL that is about to be downloaded is an EDD package URL. If so, hook our function to shorten
* the filename.
*
* @param mixed $return
* @param string $package The URL of the file to be downloaded.
*
* @return mixed
*/
function mu_lzb_pro_maybe_shorten_edd_filename( $return, $package ) {
if( strpos( $package, '/edd-sl/package_download/' ) !== false ) {
add_filter( 'wp_unique_filename', 'mu_lzb_pro_shorten_edd_filename', 10, 2 );
}
return $return;
}
/**
* Truncate the temporary filename to 50 characters. This resolves issues with some Windows servers which
* can not handle very long filenames.
*
* @param string $filename
* @param string $ext
*
* @return string
*/
function mu_lzb_pro_shorten_edd_filename( $filename, $ext ) {
$filename = substr( $filename, 0, 50 ) . $ext;
remove_filter( 'wp_unique_filename', 'mu_lzb_pro_shorten_edd_filename', 10 );
return $filename;
}
Once that file is saved, the Must Use Plugin will be active, and it will essentially truncate the long signed URL to be something that Windows servers can manage.
Please note: this truncation will happen for Lazy Blocks Pro and any other plugins you’re using that are updated using Easy Digital Downloads Software Licensing solution. This is a popular platform for many WordPress products and it should not interfere in any way, but it needs to be noted.