• I have a custom upload folder already set and upgraded WordPress to 3.5.1. I was building my website and decided to finally upload some test images and realized that WordPress will create images in the defined directory, but is unable to access it. Printing out the wp_get_attachment_image_src array shows it’s pointing to the correct path, but is returning a 404. When I try to delete the image through the media manager, it cannot physically delete the images.

    WordPress is installed in the root directory and I set the uploads folder to another folder in the root directory. I am using the Roots Theme which sets the upload directory to the root (which is also what I want) with functions.

    When I change the upload folder back to default, the option to modify the setting disappeared, but newly uploaded images work.

    It is my understanding that it was only the UI to modify the setting was removed from new versions of WordPress.

    I tried to define the directory in wp-config and set the admin option to blank to remove the UI, but same thing. WordPress cannot display the images in the admin panel or on the site because of 404 errors. It can, however, delete the files physically when I do it through media manager.

Viewing 3 replies - 1 through 3 (of 3 total)
  • This will filter your upload directory and set it to site_root/assets

    add_filter( 'upload_dir', 'upload_dir_filter' );
    
    function upload_dir_filter( $upload ) {
    	$dir = $_SERVER['DOCUMENT_ROOT'];
    	$upload_dir = $dir['basedir'] . '/assets';
    	$upload_url = $dir['baseurl'] . '/assets';
    
    	wp_mkdir_p( $upload_dir );  //WordPress will check if the dir exists and can write to it.
    	$upload['path'] = $upload_dir;
    	$upload['url']  = $upload_url;
    
    		return $upload;
    	}
    Thread Starter gavsiu

    (@gavsiu)

    Didn’t seem to do anything. The url WordPress spits out should work. I even double checked with an FTP explorer. /assets/ is in the root and the images exist as was created by WordPress.

    Thread Starter gavsiu

    (@gavsiu)

    I believe this issue may be caused by not being able to write to parent directory.

    I’m going to keep the upload folder as wp-content/uploads and use htaccess to redirect /assets/ to wp-content/uploads.

    I’m just going to use this:

    function assets_path($fullpath) {
      $upload_dir = wp_upload_dir();
      $replaced_path = str_replace($upload_dir['baseurl'], '/assets', $fullpath);
      return $replaced_path;
    }

    and add a rewrite for /assets/ to /wp-content/uploads/

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘setting custom upload folder’ is closed to new replies.