admin管理员组

文章数量:1125624

I will ask for help as I'm going nuts trying to figure out what is going on here. I've read many other questions but there is never a final answer.

I want to organize my WP installation in a different way:

public_html/
-> content/
|--> plugins/
|--> themes/
-> core/
|--> wp-includes/
|--> wp-admin/
|--> etc
-> uploads/
-> wp-config.php 
-> index.php

This structure is based and adapted from this article and WordPress-Skeleton.

Everything is fine until the moment I want to specify the UPLOADS directory. If I didn't try to, it would be inside content. But in the moment I define it, it becomes a directory inside -> core/.

The behaviour seems to be erratic, as I had it working at the beginning, and then it stopped working. Cache is disabled. This is my wp-config.php (the part to be edited):

/**
 * For developers: WordPress debugging mode.
 *
 * Change this to true to enable the display of notices during development.
 * It is strongly recommended that plugin and theme developers use WP_DEBUG
 * in their development environments.
 *
 * For information on other constants that can be used for debugging,
 * visit the documentation.
 *
 * @link /
 */
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );

define( 'WP_ENVIRONMENT_TYPE', 'staging' );
define( 'WP_DEVELOPMENT_MODE', 'all' );

define( 'WP_CACHE', false );

/* Add any custom values between this line and the "stop editing" line. */

define( 'WP_SITEURL', '' );
define( 'WP_HOME', '' );

define( 'WP_MEMORY_LIMIT', '256M' );

define( 'WP_CONTENT_DIR', dirname( __FILE__ ) . '/content' );
define( 'WP_CONTENT_URL', 'https://' . $_SERVER['HTTP_HOST'] . '/content' );

define( 'UPLOADS', 'uploads' );

/* That's all, stop editing! Happy publishing. */

/** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) ) {
    define( 'ABSPATH', __DIR__ . '/' );
}
/** Sets up WordPress vars and included files. */
require_once ABSPATH . 'wp-settings.php';

And there are two important WP options to mention as well: upload_url_path and upload_path.

upload_url_path = 

The value for this first one makes sense, right? It's the URL I want for my uploads folder (outside wp-content, named content in my case).

upload_path = uploads

And then this value is supposed to be the same thing as the UPLOADS constant defined in wp-config.php. Default value is wp-content/uploads, so if I want the uploads folder at the same level as the wp-content folder, giving it the value uploads seems to make sense.

This can be argued, because I think it is also supposed to be part of the ABSPATH (the WordPress directory), and since the content directory has been moved above the ABSPATH the relative path may not be the same anymore.

Anything I declare in define( 'UPLOADS', 'uploads' ); is being appended to /. So trying to navigate from there with something such as define( 'UPLOADS', '../uploads' ); ends up as this:

/../uploads/2024/02/image.png

which... we don't want. It works though! But it doesn't look good and seems dodgy.

Is there any clear and concise way to move the uploads directory above the WordPress installation directory and outside the content directory?

I will ask for help as I'm going nuts trying to figure out what is going on here. I've read many other questions but there is never a final answer.

I want to organize my WP installation in a different way:

public_html/
-> content/
|--> plugins/
|--> themes/
-> core/
|--> wp-includes/
|--> wp-admin/
|--> etc
-> uploads/
-> wp-config.php 
-> index.php

This structure is based and adapted from this article and WordPress-Skeleton.

Everything is fine until the moment I want to specify the UPLOADS directory. If I didn't try to, it would be inside content. But in the moment I define it, it becomes a directory inside -> core/.

The behaviour seems to be erratic, as I had it working at the beginning, and then it stopped working. Cache is disabled. This is my wp-config.php (the part to be edited):

/**
 * For developers: WordPress debugging mode.
 *
 * Change this to true to enable the display of notices during development.
 * It is strongly recommended that plugin and theme developers use WP_DEBUG
 * in their development environments.
 *
 * For information on other constants that can be used for debugging,
 * visit the documentation.
 *
 * @link https://wordpress.org/documentation/article/debugging-in-wordpress/
 */
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );

define( 'WP_ENVIRONMENT_TYPE', 'staging' );
define( 'WP_DEVELOPMENT_MODE', 'all' );

define( 'WP_CACHE', false );

/* Add any custom values between this line and the "stop editing" line. */

define( 'WP_SITEURL', 'https://staging.mywebsite.com/core' );
define( 'WP_HOME', 'https://staging.mywebsite.com' );

define( 'WP_MEMORY_LIMIT', '256M' );

define( 'WP_CONTENT_DIR', dirname( __FILE__ ) . '/content' );
define( 'WP_CONTENT_URL', 'https://' . $_SERVER['HTTP_HOST'] . '/content' );

define( 'UPLOADS', 'uploads' );

/* That's all, stop editing! Happy publishing. */

/** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) ) {
    define( 'ABSPATH', __DIR__ . '/' );
}
/** Sets up WordPress vars and included files. */
require_once ABSPATH . 'wp-settings.php';

And there are two important WP options to mention as well: upload_url_path and upload_path.

upload_url_path = https://staging.mywebsite.com/uploads

The value for this first one makes sense, right? It's the URL I want for my uploads folder (outside wp-content, named content in my case).

upload_path = uploads

And then this value is supposed to be the same thing as the UPLOADS constant defined in wp-config.php. Default value is wp-content/uploads, so if I want the uploads folder at the same level as the wp-content folder, giving it the value uploads seems to make sense.

This can be argued, because I think it is also supposed to be part of the ABSPATH (the WordPress directory), and since the content directory has been moved above the ABSPATH the relative path may not be the same anymore.

Anything I declare in define( 'UPLOADS', 'uploads' ); is being appended to https://staging.mywebsite.com/core/. So trying to navigate from there with something such as define( 'UPLOADS', '../uploads' ); ends up as this:

https://staging.mywebsite.com/core/../uploads/2024/02/image.png

which... we don't want. It works though! But it doesn't look good and seems dodgy.

Is there any clear and concise way to move the uploads directory above the WordPress installation directory and outside the content directory?

Share Improve this question asked Feb 7, 2024 at 5:20 Gerard RechesGerard Reches 2331 silver badge10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Now, do not ask me why, but the UPLOADS constant the upload_path option seem to work different.

While declaring define( 'UPLOADS', '../uploads' ); generates this:

https://staging.mywebsite.com/core/../uploads/2024/02/image.png

Removing that declaration and filling the upload_path option instead with the same value ../uploads gives a different result:

https://staging.mywebsite.com/uploads/2024/02/image.png

with the images being uploaded in the path I wanted and the media URLs well formatted.

This is the working setup and I hope the behaviour never randomly changes:

Structure

Requires setting up a copy in the main directory of the index.php found in the WordPress directory.

public_html/
-> content/
|--> plugins/
|--> themes/
-> core/
|--> wp-includes/
|--> wp-admin/
|--> etc
-> uploads/
-> wp-config.php 
-> index.php

Main index.php

<?php
define( 'WP_USE_THEMES', true );
require( './core/wp-blog-header.php' );

wp-config.php

Only the important part, of course you need the rest of your file with your database settings and everything. Do NOT declare the UPLOADS constant or it will mess everything up.


/* Add any custom values between this line and the "stop editing" line. */

define( 'WP_SITEURL', 'https://staging.mywebsite.com/core' );
define( 'WP_HOME', 'https://staging.mywebsite.com' );

define( 'WP_CONTENT_DIR', dirname( __FILE__ ) . '/content' );
define( 'WP_CONTENT_URL', 'https://' . $_SERVER['HTTP_HOST'] . '/content' );

// NO. DON'T. DO NOT EVER DECLARE IT. 
//define( 'UPLOADS', '../uploads' );

/* That's all, stop editing! Happy publishing. */

/** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) ) {
    define( 'ABSPATH', __DIR__ . '/' );
}
/** Sets up WordPress vars and included files. */
require_once ABSPATH . 'wp-settings.php';

Database options

Most important part. And risky, as they may not always exist in the wp_options table, as well as they may be overwritten by the wrong value when migrating websites or restoring DB backups.

Remember to pay attention to them. If the value of any of them is different than an empty string, you will be able to edit them from Settings -> Media -> Uploading Files. If you are having trouble again with the uploads directory or URLs and they have disappeared from that settings section, is because their values have changed back to empty string or they have been deleted. You will need to use phpMyAdmin or any other tool to add/edit them again.

The right values:

upload_url_path = https://staging.mywebsite.com/uploads
upload_path = ../uploads

I hope this can help other people to not go insane trying to figure out why it isn't working, and I will surely be coming back to this as well.

本文标签: optionsMoving uploads directory above wordpress directory UPLOADS location in parent directory