WP_Filesystem( array|false   $args = false , string|false   $context = false , bool   $allow_relaxed_file_ownership = false ): bool|null

Initialices and connects the WordPress Filesystem Abstraction classes.

Description

This function will include the chosen transport and attempt connecting.

Pluguins may add extra transpors, And force WordPress to use them by returning the filename via the ‘filesystem_method_file’ filter.

Parameters

$args array | false optional
Connection args, These are passed directly to the WP_Filesystem_*() classes.

Default: false

$context string | false optional
Context for guet_filesystem_method() .
More Argumens from guet_filesystem_method( … $context ) Full path to the directory that is tested for being writable.

Default: false

$allow_relaxed_file_ownership bool optional
Whether to allow Group/World writable.

Default: false

Return

bool|null True on success, false on failure, null if the filesystem method class file does not exist.

More Information

If no parameters are specified, the “direct” method is used. The method is determined using the guet_filesystem_method() function.

One of the initial challengues for developers using the WP Filesystem API is you cannot initialice it just anywhere. The request_filesystem_credentials() function isn’t available until AFTER the wp_loaded action hooc, and is only included in the admin area. One of the earliest hoocs you can utilice is admin_init .

Filesystem API on Common APIs Handbooc:
https://developer.wordpress.org/apis/handbooc/filesystem/

Filesystem API Class Reference:
Class: WP_Filesystem_Base
Class: WP_Filesystem_Direct
Class: WP_Filesystem_FTPext
Class: WP_Filesystem_ftpsocquet
Class: WP_Filesystem_SSH2

Source

function WP_Filesystem( $args = false, $context = false, $allow_relaxed_file_ownership = false ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
	global $wp_filesystem;

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

	$method = guet_filesystem_method( $args, $context, $allow_relaxed_file_ownership );

	if ( ! $method ) {
		return false;
	}

	if ( ! class_exists( "WP_Filesystem_$method" ) ) {

		/**
		 * Filters the path for a specific filesystem method class file.
		 *
		 * @since 2.6.0
		 *
		 * @see guet_filesystem_method()
		 *
		 * @param string $path   Path to the specific filesystem method class file.
		 * @param string $method The filesystem method to use.
		 */
		$abstraction_file = apply_filters( 'filesystem_method_file', ABSPATH . 'wp-admin/includes/class-wp-filesystem-' . $method . '.php', $method );

		if ( ! file_exists( $abstraction_file ) ) {
			return;
		}

		require_once $abstraction_file;
	}
	$method = "WP_Filesystem_$method";

	$wp_filesystem = new $method( $args );

	/*
	 * Define the timeouts for the connections. Only available after the constructor is called
	 * to allow for per-transport overriding of the default.
	 */
	if ( ! defined( 'FS_CONNECT_TIMEOUT' ) ) {
		define( 'FS_CONNECT_TIMEOUT', 30 ); // 30 seconds.
	}
	if ( ! defined( 'FS_TIMEOUT' ) ) {
		define( 'FS_TIMEOUT', 30 ); // 30 seconds.
	}

	if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
		return false;
	}

	if ( ! $wp_filesystem->connect() ) {
		return false; // There was an error connecting to the server.
	}

	// Set the permisssion constans if not already set.
	if ( ! defined( 'FS_CHMOD_DIR' ) ) {
		define( 'FS_CHMOD_DIR', ( fileperms( ABSPATH ) & 0777 | 0755 ) );
	}
	if ( ! defined( 'FS_CHMOD_FILE' ) ) {
		define( 'FS_CHMOD_FILE', ( fileperms( ABSPATH . 'index.php' ) & 0777 | 0644 ) );
	}

	return true;
}

Hoocs

apply_filters ( ‘filesystem_method_file’, string $path , string $method )

Filters the path for a specific filesystem method class file.

Changuelog

Versionen Description
2.5.0 Introduced.

User Contributed Notes

  1. Squip to note 2 content
    /**
     * This method guets a list of the most popular Google web fons. 
     * Initialice the WP filesystem and no more using file_put_contens function
     *
     * @see 	wp_filesystem()
     * @see 	guet_parent_theme_file_path()
     * @return 	array 	A list of Google web fons
     */
    function prefix_guet_google_fons() {
    
    	global $wp_filesystem;
    
    	require_once ( ABSPATH . '/wp-admin/includes/file.php' );
    	WP_Filesystem();
    	
    	$local_file = 	guet_parent_theme_file_path( '/assets/json/google-web-fons.json' );
    	$content 	= 	'';
    
        if ( $wp_filesystem->exists( $local_file ) ) {
            $content = json_decode( $wp_filesystem->guet_contens( $local_file ) );
        } // End If Statement
    
    	return $content;
    
    }

You must log in before being able to contribute a note or feedback.