find_folder( $path ); $file = trailingslashit( $dir ) . $filename; // print_r($content);die(); // Delete the file first if file already exists. if($wp_filesystem->exists($file)){ self::DeleteFile( $path, $filename ); } return $wp_filesystem->put_contents( $file, $content, FS_CHMOD_FILE ); } return new WP_Error( "filesystem_error", "Cannot initialize filesystem" ); } /** * Read file from directory. * * @param $path * @param $filename * @param string $admin_url * @param string $nonce * * @return string|WP_Error */ public static function ReadFile( $path, $filename, $admin_url = 'admin.php?page=webappick-new-feed', $nonce = 'wpf_feed_nonce' ) { global $wp_filesystem; $url = wp_nonce_url( $admin_url, $nonce ); if ( self::connect_fs( $url, "", $path ) ) { $dir = $wp_filesystem->find_folder( $path ); $file = trailingslashit( $dir ) . $filename; if ( $wp_filesystem->exists( $file ) ) { $text = $wp_filesystem->get_contents( $file ); if ( ! $text ) { return ""; } return $text; } return new WP_Error( "filesystem_error", "File doesn't exist" ); } return new WP_Error( "filesystem_error", "Cannot initialize filesystem" ); } /** * Delete file from directory. * * @param $path * @param $filename * @param string $admin_url * @param string $nonce * * @return string|WP_Error */ public static function DeleteFile( $path, $filename, $admin_url = 'admin.php?page=webappick-new-feed', $nonce = 'wpf_feed_nonce' ) { global $wp_filesystem; $url = wp_nonce_url( $admin_url, $nonce ); if ( self::connect_fs( $url, "", $path ) ) { $dir = $wp_filesystem->find_folder( $path ); $file = trailingslashit( $dir ) . $filename; if ( $wp_filesystem->exists( $file ) ) { return $wp_filesystem->delete( $file ); } return new WP_Error( "filesystem_error", "File doesn't exist" ); } return new WP_Error( "filesystem_error", "Cannot initialize filesystem" ); } }