Skip to content

Instantly share code, notes, and snippets.

@widoz
Last active April 11, 2018 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save widoz/639d16724606bddcaffa to your computer and use it in GitHub Desktop.
Save widoz/639d16724606bddcaffa to your computer and use it in GitHub Desktop.
Wp Filesystem
<?php
/**
* Switch Upload Dir & Url
*
* @since 1.0.0
*
* @param string $file The file path.
* @param string $switch The reference to the string to replace. Allowed 'dir>url', 'url>dir'.
*
* @return string The file url
*/
function xxx_switch_upload_dir_path_url( $file, $switch = 'dir>url' ) {
// Get upload dir data.
$_upload_dir = wp_upload_dir();
// Initialize the new file path.
$_file = '';
if ( 'dir>url' === $switch ) {
$_file = str_replace( $_upload_dir['basedir'], $_upload_dir['baseurl'], $file );
} else if ( 'url>dir' === $switch ) {
$_file = str_replace( $_upload_dir['baseurl'], $_upload_dir['basedir'], $file );
} else {
$_file = $file;
}
if ( $_file ) {
return $_file;
}
}
<?php
add_filter('upload_size_limit', function ($byte)
{
// 10MB
return 10000000;
});
<?php
/**
* WP Direct FileSystem instance
*/
function wpFilesystem()
{
if (!class_exists('WP_Filesystem_Direct')) {
require_once untrailingslashit(ABSPATH) . '/wp-admin/includes/class-wp-filesystem-base.php';
require_once untrailingslashit(ABSPATH) . '/wp-admin/includes/class-wp-filesystem-direct.php';
}
return new \WP_Filesystem_Direct(true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment