Redux has a WordPress filesystem proxy built in to help you read and write files that will pass Theme-Check. You can use these calls the moment a Redux object has been loaded (your Redux config has been run).
Getting Started
First, you must get the Redux object. The easiest method is as follows:
$redux = ReduxFrameworkInstances::get_instance('opt_name');
Now that you have the object, you can begin using the filesystem. It’s pretty easy to do, you run the following call:
$redux->filesystem->execute( 'action', PATH, $args );
Below you will find a list of all possible calls, and the arguments associated with each.
Chmod / File Permissions
By default the WP_FileSystem API uses default values for chmod (read/write permissions). These are in the form of the declared FS_CHMOD_DIR for directories or FS_CHMOD_FILE for files. Do not worry about setting these values yourself.
If you MUST override the default chmod settings, you may pass in a chmod argument for all actions except object and unzip. To do so, pass a string or int:
0644
as this argument value.
Possible Calls & Arguments.
“mkdir”
This action allows you to generate a directory of any kind. It will even make multiple children directories even it if doesn’t exist.
Arguments
No arguments exist for this action.
Example Declaration
if ( ! is_dir( 'DIRPATH' ) ) { $redux->filesystem->execute( "mkdir", self::$_upload_dir ); }
“copy”
This allows you to copy files from one location to another.
Arguments
name | type | required | description |
destination | string | required | The destination file path. |
overwrite | boolean | optional | Overwrite the files if exist, or by default the WP_FileSystem will skip existing if the file exists. |
Example Declaration
$redux->filesystem->execute( 'copy', 'FILE_PATH', array( 'destination' => "DESTINATION_PATH" ) );
“put_contents”
The same as file_put_contents in PHP, but using the WordPress filesystem API. This will write the contents of a variable to file. This will override any file if it already exists, just like file_put_contents.
Arguments
name | type | required | description |
content | string | required | The content of the file. |
Example Declaration
$redux->filesystem->execute( 'put_contents', 'FILE_PATH', array( 'content' => "This is the content of the file." ) );
“get_contents”
This will get the contents of a filesystem in any way it can. This will attempt to use the WordPress filesystem first. However, if the file does not have the standard WordPress read permissions, it will fallback to file_get_contents.
Arguments
No arguments exist for this action.
Example Declaration
$file_contents = $redux->filesystem->execute( 'get_contents', 'FILE_PATH' );
“object”
This will simply return a valid WordPress FileSystem API object.
Arguments
No arguments exist for this action.
Example Declaration
$object = $redux->filesystem->execute( 'object' );
“unzip”
This allows you to unzip an existing file to a new location.
Arguments
name | type | required | description |
destination | string | required | The destination to unzip the zip file contents. |
Example Declaration
$redux->filesystem->execute( 'unzip', 'ZIP_FILE_PATH', array( 'destination' => 'DESTINATION_PATH' ) );