Skip to content

Instantly share code, notes, and snippets.

@lucasstark
Created February 6, 2016 16:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lucasstark/e845db3678bd7b95a2b8 to your computer and use it in GitHub Desktop.
Save lucasstark/e845db3678bd7b95a2b8 to your computer and use it in GitHub Desktop.
<?php
class ACF_File_Custom {
private static $instance;
public static function register() {
if ( self::$instance == null ) {
self::$instance = new ACF_File_Custom();
}
}
private $_fields = array();
private function __construct() {
/* Change the field key here.
* You can change the subdirectory to whatever you want as well.
* This code currently will place the files in the default uploads directory + whatever you configure here.
* So this example will result in files being in wp-content/uploads/secure/filename.ext
*/
$this->_fields['field_56b60ed95cb71'] = '/secure'; //Change the directory to whatever you want.
//$this->_fields['another_field_key'] = '/another-secure-directory';
add_filter( 'upload_dir', array($this, 'change_upload_dir') );
}
public function change_upload_dir( $dir ) {
if (isset($_POST['_acfuploader']) && !empty($_POST['_acfuploader'])){
$key = $_POST['_acfuploader'];
if (array_key_exists($key, $this->_fields)){
/*
* The default $dir is as follows.
[path] => (string) /Applications/XAMPP/xamppfiles/htdocs/local.wordpress.edu/wp-content/uploads/2016/02
[url] => (string) http://local.wordpress.com/wp-content/uploads/2016/02
[subdir] => (string) /2016/02
[basedir] => (string) /Applications/XAMPP/xamppfiles/htdocs/local.wordpress.edu/wp-content/uploads
[baseurl] => (string) http://local.wordpress.com/wp-content/uploads
[error] => (bool) false
*/
$destination_folder_url = $this->_fields[$key];
$dir['basedir'] = $dir['basedir'] . $destination_folder_url;
$dir['baseurl'] = $dir['baseurl'] . $destination_folder_url;
$dir['subdir'] = $destination_folder_url;
$dir['url'] = $dir['baseurl'] . $destination_folder_url;
$dir['path'] = $dir['basedir'];
}
}
return $dir;
}
}
ACF_File_Custom::register();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment