Home Forums General Issues ACF with custom upload paths


ACF with custom upload paths

  • So I have a custom upload script in my functions.php file and for some reason ACF just does not like it. I have tried a few things but nothing is working. I am hoping somewhere here has been able to solve a similar problem.

    Here is what I have in my functions.php

    add_filter('wp_handle_upload_prefilter', 'spacepad_pre_upload', 2);
    add_filter('wp_handle_upload', 'spacepad_post_upload', 2);
    // Change the upload path to the one we want
    function spacepad_pre_upload($file){
        add_filter('upload_dir', 'spacepad_custom_upload_dir');
        return $file;
    // Change the upload path back to the one WordPress uses by default
    function spacepad_post_upload($fileinfo){
        remove_filter('upload_dir', 'spacepad_custom_upload_dir');
        return $fileinfo;
    function spacepad_custom_upload_dir($path){    
         * Determines if uploading from inside a post/page/cpt - if not, default Upload folder is used
        $use_default_dir = ( isset($_REQUEST['post_id'] ) && $_REQUEST['post_id'] == 0 ) ? true : false; 
        if( !empty( $path['error'] ) || $use_default_dir )
            return $path; //error or uploading not from a post/page/cpt 
    	* Save uploads in SLUG based folders 
    	$the_cat = get_the_category( $_REQUEST['post_id'] );
    	// If there is a category lets use that for our organization
    	if ( $the_cat ){
    		$customdir = '/' . strtolower(str_replace(" ", "-", $the_cat[0]->cat_name));
    	} else {
    		$post_data = get_post($_REQUEST['post_id'], ARRAY_A);
    		$slug = $post_data['post_name'];
    		$customdir = '/' . $slug;	
        $path['path']    = str_replace($path['subdir'], '', $path['path']); //remove default subdir (year/month)
        $path['url']     = str_replace($path['subdir'], '', $path['url']);      
        $path['subdir']  = $customdir;
        $path['path']   .= $customdir; 
        $path['url']    .= $customdir;  
        return $path;

    The idea is that if a page or a post has a category assigned to it all files will be uploaded into a specific category folder. If there is no category it should be uploaded to the page slug folder.

    The files are uploading fine and in the right spot but ACF doesnt seem to store this information or access it properly and I am constantly getting no result when I call the_field();.

    Even the WordPress media library is showing it fine. It is just ACF calls to it that fail.

  • Hi @thorak

    Does ACF show the correct image in the backend? Perhaps the issue is not with ACF, but with your code on the front end.

    Have you debugged the result of get_field()?

    Have you made sure the return value option is what you expect?
    Have you tried ID, Object and URL?

  • Hey Elliot,

    Thanks for the reply. As far as all your questions are concerned. I have run quite a few tests already of which using the ID, Object and URL were part of that.

    In the admin side of things I can select the image through the ACF field from both the media library or upload a new one. If I upload a new one it get uploaded to the custom directory I assigned in my plugin file just fine. It even shows in the media library as well. And I can use the basic WP Media Insert button to insert the file into the post content. This works and displays on the admin and client sides.

    What is interesting is that when I select the file with ACF (using the ‘select’ button in the media library) the file seems to be assigned to the post / page. However if I leave the page and come back to it the ACF field is blank again.

    On the client / user side the file never seems to appear. It has a null value.

    As far as debuging: “Have you debugged the result of get_field()?” I have tried several of my own methods – including checking the DB fields with phpMyAdmin and in all cases it seems to be a null value for ACF. If you have a specific test you could suggest I would be more than happy to test it. I am quite adept at PHP and WordPress.

  • I should add that I am running ACF and WP 3.6

    I am also using the file feature of ACF on several other basic posts and pages all with no issues as well.

    I am starting to wonder if it may be related to the order of the add_action functions. Maybe I can run some test like that today to see if it makes a difference.

    Sorry what I meant to say is that I am using this feature on OTHER websites with basic posts and pages. And they all work fine.

  • Ok this problem is resolved. I took a new approach with a fresh mind.

    I searched the database for ALL meta_key’s with a value LIKE ‘resume’ (the name of the ACF field in question). I figured it was worth doing instead of using the ‘=’ operand.

    I found 2 references. One as ‘Resume’ and one as ‘resume’. The ‘resume’ key was from ACF and the ‘Resume’ key was an old key from a year ago before I switched over to ACF. It had a value of null. The ‘resume’ field had an ID value.

    Interestingly enough as soon as I deleted the ‘Resume’ record ACF started to work just fine.

    I know its an oddball situation but there was obviously some conflict with ACF getting the field from the DB with the same name but different case settings. Worth you looking into still.

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘ACF with custom upload paths’ is closed to new replies.