Home › Forums › Backend Issues (wp-admin) › Can't get upload_prefilter to work?
Hey there.
What I need is to place my uploaded files in another folder than the upload directory. For this purpose, I have found the following piece of code multiple places in the forums:
function my_acf_upload_prefilter( $errors, $file, $field ) {
// only allow admin
if( !current_user_can('manage_options') ) {
// this returns value to the wp uploader UI
// if you remove the ! you can see the returned values
$errors[] = 'test prefilter';
$errors[] = print_r($_FILES,true);
$errors[] = $_FILES['async-upload']['name'] ;
}
//this filter changes directory just for item being uploaded
add_filter('upload_dir', 'my_upload_directory');
// return
return $errors;
}
add_filter('acf/upload_prefilter/name=kontrakt_file', 'my_acf_upload_prefilter');
function my_upload_directory( $param ){
$mydir = '/newdirectory';
$param['path'] = $param['basedir'] . $mydir;
$param['url'] = $param['baseurl'] . $mydir;
// if you need a different location you can try one of these values
/*
error_log("path={$param['path']}");
error_log("url={$param['url']}");
error_log("subdir={$param['subdir']}");
error_log("basedir={$param['basedir']}");
error_log("baseurl={$param['baseurl']}");
error_log("error={$param['error']}");
*/
return $param;
}
However, it doesn’t work for me. I have placed the code in my wordpress’ functions.php, created a directory named newdirectory inside my uploads directory, and the name of the field i am targeting is kontrakt_file.
Can someone please help me out, before I finished ripping all my hair out from my head? 😉
Might be a stupid question – but is it because I need pro version to use filters?
Hi @cc
That’s weird. I’ve just tested that code and it was working great on my end. It’s possible that there’s a conflict on your site. Could you please try to reproduce the issue on one of the WordPress’ stock themes (like Twenty Sixteen) with other plugins deactivated? If it disappears, then you can activate the theme and plugins one by one to see which one causes the issue.
I’m sorry I didn’t notice that you use the free version. I’m afraid the “acf/upload_prefilter” is only available in the PRO version. In this case, you need to use the wp_handle_upload_prefilter, but you need to use extra logic to determine if the images are posted from a ACF or not.
Thanks 🙂
Thanks. I’ve been wanting to buy PRO anyway, so that ain’t a problem 🙂
Another question; can I somehow insert a “custom script field” in the backend? Ie, if I have created a tab, and under that tab, I would like to retrieve some comments based on specific metadata, or similar that requires coding. Could I insert something like that?
Hi @cc
Yes, you can insert Javascript code by using an action hook like acf/input/admin_footer. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/adding-custom-javascript-fields/.
If you want to add PHP code, you can use other action hooks like acf/render_field. Please check this page to check the other hooks: https://www.advancedcustomfields.com/resources/#actions.
I hope this helps 🙂
Thank you very much for your big help. I have upgraded to PRO and everything seems to be working now 🙂
I still need one small thing though. I have suceeded placing the uploaded files in a subdir to /uploads/ by using the mentioned code in my topic. Can I change the $uploads[‘path’] & $uploads[‘url’], so that the uploaded files end up outside my web root/www folder? upload_dir() seems to take ../ quite literally..
Thanks!
Hi @cc
I don’t think you can do it. But to be sure, I suggest you ask the WordPress support forum instead.
Thanks 🙂
The topic ‘Can't get upload_prefilter to work?’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.