WP_Filesystem

Initialises and connects the WordPress Filesystem Abstraction classes.

Description

(null|bool) WP_Filesystem( (constant) $args = false, (constant) $context = false, (bool) $allow_relaxed_file_ownership = false ); 

This function will include the chosen transport and attempt connecting.

Plugins may add extra transports, And force WordPress to use them by returning the filename via the filter.

Returns (null|bool)

false on failure, true on success.

Parameters (3)

0. $args — Optional. (constant) => false
Connection args, These are passed directly to the WP_Filesystem_*() classes. Default false.
1. $context — Optional. (constant) => false
Context for get_filesystem_method(). Default false.
2. $allow_relaxed_file_ownership — Optional. (bool) => false
Whether to allow Group/World writable. Default false.

Usage

  1. if ( !function_exists( 'WP_Filesystem' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/file.php'; 
  3.  
  4. // Optional. Connection args, These are passed directly to 
  5. // the `WP_Filesystem_*()` classes. Default false. 
  6. $args = false; 
  7.  
  8. // Optional. Context for get_filesystem_method(). Default false. 
  9. $context = false; 
  10.  
  11. // Optional. Whether to allow Group/World writable. Default false. 
  12. $allow_relaxed_file_ownership = false; 
  13.  
  14. // NOTICE! Understand what this does before running. 
  15. $result = WP_Filesystem($args, $context, $allow_relaxed_file_ownership); 
  16.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/file.php  
  1. function WP_Filesystem( $args = false, $context = false, $allow_relaxed_file_ownership = false ) { 
  2. global $wp_filesystem; 
  3.  
  4. require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php'); 
  5.  
  6. $method = get_filesystem_method( $args, $context, $allow_relaxed_file_ownership ); 
  7.  
  8. if ( ! $method ) 
  9. return false; 
  10.  
  11. if ( ! class_exists( "WP_Filesystem_$method" ) ) { 
  12.  
  13. /** 
  14. * Filters the path for a specific filesystem method class file. 
  15. * @since 2.6.0 
  16. * @param string $path Path to the specific filesystem method class file. 
  17. * @param string $method The filesystem method to use. 
  18. */ 
  19. $abstraction_file = apply_filters( 'filesystem_method_file', ABSPATH . 'wp-admin/includes/class-wp-filesystem-' . $method . '.php', $method ); 
  20.  
  21. if ( ! file_exists($abstraction_file) ) 
  22. return; 
  23.  
  24. require_once($abstraction_file); 
  25. $method = "WP_Filesystem_$method"; 
  26.  
  27. $wp_filesystem = new $method($args); 
  28.  
  29. //Define the timeouts for the connections. Only available after the construct is called to allow for per-transport overriding of the default. 
  30. if ( ! defined('FS_CONNECT_TIMEOUT') ) 
  31. define('FS_CONNECT_TIMEOUT', 30); 
  32. if ( ! defined('FS_TIMEOUT') ) 
  33. define('FS_TIMEOUT', 30); 
  34.  
  35. if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() ) 
  36. return false; 
  37.  
  38. if ( !$wp_filesystem->connect() ) 
  39. return false; //There was anerrorconnecting to the server. 
  40.  
  41. // Set the permission constants if not already set. 
  42. if ( ! defined('FS_CHMOD_DIR') ) 
  43. define('FS_CHMOD_DIR', ( fileperms( ABSPATH ) & 0777 | 0755 ) ); 
  44. if ( ! defined('FS_CHMOD_FILE') ) 
  45. define('FS_CHMOD_FILE', ( fileperms( ABSPATH . 'index.php' ) & 0777 | 0644 ) ); 
  46.  
  47. return true;