Skip to main content
added 432 characters in body
Source Link
Sally CJ
  • 39.9k
  • 2
  • 26
  • 48

What is the best way to load the dependencies for the WP_Filesystem? The first or the second code part?

The second one is good and I would use that as well.

  1. WP_Filesystem() is used in conjunction with the global $wp_filesystem variable, so if you use it, then the second code part should be used.

    And WP_Filesystem() must be called so that the global $wp_filesystem variable is properly initialized/setup, i.e. assigned with the proper class instance or file system method (see get_filesystem_method(), which defaults to WP_Filesystem_Direct).

  2. If you're not using the global $wp_filesystem variable, then you could just use the first one to manually load the dependencies which are loaded automatically by WP_Filesystem().

And WP_Filesystem() must be called so thatExample using the global $wp_filesystem variable is properly initialized/setup, i.e. assigned with the proper class or file system method (see get_filesystem_method(), which defaults to WP_Filesystem_Direct), and therefore allowing you to use the WordPress API for file/directory operations such as checking if a file exists using $wp_filesystem->exists() or getting the file content using $wp_filesystem->get_contents().

Example:

global $wp_filesystem;

// Make sure that the above variable is properly setup.
require_once ABSPATH . 'wp-admin/includes/file.php';
WP_Filesystem();

// Check whether a file/directory exists.
$exists = $wp_filesystem->exists( '/some/path/here' );
var_dump( $exists );

// Get file content.
$data = $wp_filesystem->get_contents( 'path/to/file' );
var_dump( $data );

Example using a custom variable:

require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';
require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php';

$filesystem = new WP_Filesystem_Direct( false );

var_dump( $filesystem->exists( '/some/path/here' ) );

What is the best way to load the dependencies for the WP_Filesystem? The first or the second code part?

The second one is good and I would use that as well.

And WP_Filesystem() must be called so that the global $wp_filesystem variable is properly initialized/setup, i.e. assigned with the proper class or file system method (see get_filesystem_method(), which defaults to WP_Filesystem_Direct), and therefore allowing you to use the WordPress API for file/directory operations such as checking if a file exists using $wp_filesystem->exists() or getting the file content using $wp_filesystem->get_contents().

Example:

global $wp_filesystem;

// Make sure that the above variable is properly setup.
require_once ABSPATH . 'wp-admin/includes/file.php';
WP_Filesystem();

// Check whether a file/directory exists.
$exists = $wp_filesystem->exists( '/some/path/here' );
var_dump( $exists );

// Get file content.
$data = $wp_filesystem->get_contents( 'path/to/file' );
var_dump( $data );

What is the best way to load the dependencies for the WP_Filesystem? The first or the second code part?

  1. WP_Filesystem() is used in conjunction with the global $wp_filesystem variable, so if you use it, then the second code part should be used.

    And WP_Filesystem() must be called so that the global $wp_filesystem variable is properly initialized/setup, i.e. assigned with the proper class instance or file system method (see get_filesystem_method(), which defaults to WP_Filesystem_Direct).

  2. If you're not using the global $wp_filesystem variable, then you could just use the first one to manually load the dependencies which are loaded automatically by WP_Filesystem().

Example using the global $wp_filesystem variable:

global $wp_filesystem;

// Make sure that the above variable is properly setup.
require_once ABSPATH . 'wp-admin/includes/file.php';
WP_Filesystem();

// Check whether a file/directory exists.
$exists = $wp_filesystem->exists( '/some/path/here' );
var_dump( $exists );

// Get file content.
$data = $wp_filesystem->get_contents( 'path/to/file' );
var_dump( $data );

Example using a custom variable:

require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';
require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php';

$filesystem = new WP_Filesystem_Direct( false );

var_dump( $filesystem->exists( '/some/path/here' ) );
Source Link
Sally CJ
  • 39.9k
  • 2
  • 26
  • 48

What is the best way to load the dependencies for the WP_Filesystem? The first or the second code part?

The second one is good and I would use that as well.

And WP_Filesystem() must be called so that the global $wp_filesystem variable is properly initialized/setup, i.e. assigned with the proper class or file system method (see get_filesystem_method(), which defaults to WP_Filesystem_Direct), and therefore allowing you to use the WordPress API for file/directory operations such as checking if a file exists using $wp_filesystem->exists() or getting the file content using $wp_filesystem->get_contents().

Example:

global $wp_filesystem;

// Make sure that the above variable is properly setup.
require_once ABSPATH . 'wp-admin/includes/file.php';
WP_Filesystem();

// Check whether a file/directory exists.
$exists = $wp_filesystem->exists( '/some/path/here' );
var_dump( $exists );

// Get file content.
$data = $wp_filesystem->get_contents( 'path/to/file' );
var_dump( $data );