Pergunta

I don't think I ever had practical need to create folders/files in WP before, but for a plugin I need cache (for resized images) folder in wp-content/uploads.

Which raises the question - do I really need to go through all the process with Filesystem API (including messily asking for FTP credentials when needed), or wp_mkdir_p() is good enough for this?

Foi útil?

Solução

wp-content/uploads/ should be writable for the server (otherwise it would be impossible to upload a file, no?). If you are going to create something under this directory, it is safe to use wp_mkdir_p().

I would only use WP_Filesystem if there is a chance the server does not have permissions to write to the location, like in wp-content/plugins/, which does not have to be writable for the server (at least I think it doesn't have to be?).

Sidenote: The File Permissions page of the Codex also talks about a wp-content/cache/ directory. Would this be a "more standard" location for cache files?

Outras dicas

I'd try something simple, before messing around with an endless sea of possibilites/hosting setups:

$target = 'wherever';
wp_mkdir_p( $target );

if ( wp_mkdir_p( $target ) === TRUE )
{
    echo "Folder $target successfully created";
}
else
{
    new WP_Error; #... etc. Just tell where the user has to make a new folder with the name xy
    // or if you're kool, you use _doing_it_wrong(); ... ;-)
}

Just a heads up on having your plugin create files/folders: my plugin did that as well, and I ran into a slew of issues with file permissions for different users. Some are windows, some are unix, some are hosted, and some home grown. It was my #1 support email once I shared the plugin.

I have refactored my code to minimize the need to touch the file systems and the upload directory is the ONLY one I use now to avoid such issues.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a wordpress.stackexchange
scroll top