How to modify url of uploaded files on a site?

I've noticed upoads on edublogs don't point to a folder called 'sites' + \ + <<number-of-blog>> but they all go to files folder and then I guess the id of the blog is added at the end of the uploaded file. Is there a plugin thats does that? it's much better this way than the standard wp way. Would really appreciate if you could tell me. thanks, David

  • David

    Hi Ash, maybe I haven't been able to explain. It's not just the uploads folder, but not having subfolders for all sites in a blog network (multisite).

    Lets say I have a blog on edu blogs and I upload a file called '20130311_11542.jpg' i do it and it turns out to be in the folder '<<myblog>>.edublogs.org/files/2013/09/' and then I get my file renamed to '20130311_11542-225zmr5.jpg' on that folder where I guess '225zmr5' is s/t related to my blog's id. This means that all files uploaded in setember 2013 from all blogs hosted on edublogs are in the same folder. Right? That's precisely what I would like.

    I look at the code of the wp original media path plugin and do not think it behaves this way. Does it?

    Thanks a lot, David

  • Ash

    Hi there

    Sorry for misunderstanding. You will be able to change the whole upload directory to one folder. And the second function will rename the file during upload.

    add_filter('upload_dir', 'ml_media_upload_dir');
    function ml_media_upload_dir($upload) {
    	global $user_ID;
    	if ((int)$user_ID > 0) {
    		$upload['path'] = ABSPATH . 'wp-content/images';
    		switch_to_blog(1);
    		$upload['url'] = site_url() . '/' . 'wp-content/images';
    		restore_current_blog();
    		$upload['subdir'] = "";
    		$upload['basedir'] = $upload['path'];
    		$upload['baseurl'] = $upload['url'];
    	}
    	return $upload;
    }
    
    function make_filename_hash($filename) {
        global $user_ID;
        return $user_ID . $filename;
    }
    add_filter('sanitize_file_name', 'make_filename_hash', 10);

    Please let me know if it works for you.

    Cheers
    Ash

  • David

    Hi Ash,

    Yes I believe it will work.

    For the moment I've installed it as a mu-plugin in a single site installation just to test (commenting the lines 6 and 8) but I guess It will work just fine. Maybe with little further customizations.

    This was an issue that was really bugging me because is such and important thing controlling where uploads are sent. Also because of SEO purposes.

    By the way, I tested it on the Twenty Thirteen Theme and noticed that the attachment template has to be also modified to point attachments to the new location. But that's ok, it's logical that WordPress does not take care of it on it's own.

    Thank's a lot,

    David

  • David

    Hi Ashok,

    It's been a while, just wanted to keep you updated about this subject.

    When I wrote:

    By the way, I tested it on the Twenty Thirteen Theme and noticed that the attachment template has to be also modified to point attachments to the new location. But that's ok, it's logical that WordPress does not take care of it on it's own.

    I was wrong. The snippet works great and takes care of everything. It's just that old images have to be migrated to the new location and the database post_meta information has to be modified as well.

    I just wanted to add some information:

    I use multisite to deploy multi language sites. I do have one site with WPML but I thing Multisite is better for SEO.

    So, the first time I tried this snippet (which I installed as a mu-plugin) on a production site I experienced problems with anonymous users (thats is not logged in users): WordPress did not compute the right path to the upload folder. I was not able to reproduce this problem in my development environment since I was all the time logged in, but once I deployed the new site, I realized about the problem.

    The solution was to skip the condition:

    if ((int)$user_ID > 0) {

    Once this line of code is removed, everything is OK.

    Just wanted to let you know.

    Best regards,

    David B

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.