What dependencies should I load and to use the WP_Filesystem?

I’m building a WordPress theme which has a WP_Filesystem call in it:

<?php $wp_filesystem = new WP_Filesystem_Direct(null);
        echo $wp_filesystem->get_contents(get_stylesheet_directory() . '/assets/images/Search_Glyph.svg'); ?>

I know that the WP_Filesystem needs some dependencies. I load them now like this in functions.php:

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

I don’t know if this is the correct way to load the dependencies for the filesystem. I found an other question for plugin development and after some research I made the following code, which also works:

require_once ( ABSPATH . '/wp-admin/includes/file.php' );
WP_Filesystem();

What is the best way to load the dependencies for the WP_Filesystem? The first or the second code part? Or is there other code which I should use?

To me it seems the second, because WP_Filesystem() is a WordPress function, but I don’t know what it does exactly.

Many thanks in advance!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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' ) );


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x