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

Initialises and connects the WordPress Filesystem Abstraction classes.


Description Description

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 ‘filesystem_method_file’ filter.


Parameters Parameters

$args

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

Default value: false

$context

(string|false) (Optional) Context for get_filesystem_method().

Default value: false

$allow_relaxed_file_ownership

(bool) (Optional) Whether to allow Group/World writable.

Default value: false


Top ↑

Return Return

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


Top ↑

Source Source

File: wp-admin/includes/file.php

1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
function WP_Filesystem( $args = false, $context = false, $allow_relaxed_file_ownership = false ) {
    global $wp_filesystem;
 
    require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' );
 
    $method = get_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 get_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 construct is called to allow for per-transport overriding of the default.
    if ( ! defined( 'FS_CONNECT_TIMEOUT' ) ) {
        define( 'FS_CONNECT_TIMEOUT', 30 );
    }
    if ( ! defined( 'FS_TIMEOUT' ) ) {
        define( 'FS_TIMEOUT', 30 );
    }
 
    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 permission constants 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;
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.5.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Mahdi Yazdani
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    /**
     * This method gets a list of the most popular Google web fonts.
     * Initialize the WP filesystem and no more using file_put_contents function
     *
     * @see     wp_filesystem()
     * @see     get_parent_theme_file_path()
     * @return  array   A list of Google web fonts
     */
    function prefix_get_google_fonts() {
     
        global $wp_filesystem;
     
        require_once ( ABSPATH . '/wp-admin/includes/file.php' );
        WP_Filesystem();
         
        $local_file =   get_parent_theme_file_path( '/assets/json/google-web-fonts.json' );
        $content    =   '';
     
        if ( $wp_filesystem->exists( $local_file ) ) {
            $content = json_decode( $wp_filesystem->get_contents( $local_file ) );
        } // End If Statement
     
        return $content;
     
    }

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