[Solved] Changing media upload directory doesn't work

I’m just looking into roots and enabled the option to change the upload directory to “media” but this poses a small problem. I’ve organised my project structure (as shown in the Composer blogpost) to have all WP core files in the “wp” directory and moved the “wp-content” directory outside of the “wp” directory. Any plugins and themes are now located outside of the “wp” directory.

Problem is that the “media” directory is now located inside the “wp” directory instead of alongside the “wp” and “wp-content” directories.

The relevant part of the configuration (I think) is:

/* Run WordPress from a subdirectory */
define('WP_SITEURL', 'http://' . $_SERVER['SERVER_NAME'] . '/roots/wp');
define('WP_HOME',    'http://' . $_SERVER['SERVER_NAME'] . '/roots');

/* Separate the wp-content directory from the WordPress install */
define('WP_CONTENT_DIR', $_SERVER['DOCUMENT_ROOT'] . '/roots/wp-content');
define('WP_CONTENT_URL', 'http://' . $_SERVER['SERVER_NAME'] . '/roots/wp-content');

From another topic I added the following lines:

<?php echo '<p>WP_CONTENT_URL:'.WP_CONTENT_URL.'</p>'; ?>
<?php echo '<p>WP_CONTENT_DIR:'.WP_CONTENT_DIR.'</p>'; ?>
<?php echo '<p>UPLOAD_PATH:'.get_option( 'upload_path' ).'</p>'; ?>
<?php echo '<p>UPLOAD_URL_PATH:'.get_option( 'upload_url_path' ).'</p>'; ?> 

This resulted in:

WP_CONTENT_URL:http://filevault/roots/wp-content
WP_CONTENT_DIR:/usr/local/www/htdocs/roots/wp-content
UPLOAD_PATH:media
UPLOAD_URL_PATH:

Any suggestions on how to fix this and get the “media” directory in the root along with the “wp” and “wp-content” directories?

Doing some more research I found the following solution in a comment by K. Adam White in the following posting:

How to Change the Default Media Upload Location in WordPress 3.5

/* Fix media directory location. */
if ( empty( $upload_path ) || 'wp-content/uploads' == $upload_path ) {
	update_option( 'upload_path', untrailingslashit( str_replace( 'wp', 'media', ABSPATH ) ) );
	update_option( 'upload_url_path', home_url( '/media' ) );
}

Change ‘wp’ to whatever directory you use for the WP core files, and ‘media’ with the name of the upload directory.

3 Likes

Thanks for this. I haven’t started using Composer quite yet. However I had a related question as to how is the rewrite for changing to the /media/ directory handled? Is this added to the .htaccess folder?

I’ve been having a few issues on a number of sites where I get loads of 404s because the site thinks the image files are in the wrong directory. I’ve got confused as to whether they should be in /media/ or /assets/ in the root directory (i.e. not in the theme directory)

The change of upload folder is not handled by a rewrite rule but rather internally by WordPress.

You can use the following rules (that is, I use these rules): things that are needed to display your site properly should be in the assets folder, anything content related should be in the media folder.

@kdb Here’s an mu-plugin we’re using for the roots.io site that sets up anything you normally can’t do in the config automatically: https://gist.github.com/swalkinshaw/6400708

This also includes a similar solution for changing the uploads folder to media.

2 Likes

This is great! Thanks, I will be adding an option to disable organizing uploads into year/month folders

update_option('uploads_use_yearmonth_folders', 0);
1 Like

@swalkinshaw so, when using composer, my media folder ends up /wp/media when I activate roots. What steps need to happen to get it to be in a different location?

The mu-plugin in the Gist I linked above solves that problem.

I added the code to /wp-content/mu-plugins/. Nothing changed. Using Windows, but I am not sure that makes a diff.

I’d make sure it actually ran by checking the updated options in the database. Or just put some debugging code in that if statement to make sure it executes. That would be the first thing to confirm.

Ok, so here is the issue. I use port numbers for my development sites, so _SERVER['SERVER_NAME'] resolves to the server name only (i.e localhost:8080 would be localhost). It never modified the paths.

##** SOLUTION
Updated code for those of you with same issue: https://gist.github.com/talves/7346027

2 Likes

Awesome, thanks! Glad you figured it out.

Dear @swalkinshaw,

just wondering if this is still the best option to set the folder structure for multisite. Facing the same issue still in 2018 that the UPLOADSconstant is relative to ABSPATH:

Thank you and best regards!