php - Custom location for attached images

I managed to upload images out of where blog (WordPress) is installed, and I'm wondering to see how can I update de

I managed to upload images out of where blog (WordPress) is installed, and I'm wondering to see how can I update default attached image path.

Few facts:

  • The main website is installed on docker. main
  • There is another stand alone WordPress website (blog) installed on docker: main/blog
  • There is a subdomain on separate docker where I keep blog/wp-content: media.main
  • UPLOAD directory for blog is out of wp-content directory and I can SSH to it by going to where the subdomain docker is: Docker/doc/media.main/blog/files
  • wp-content for blog is renamed to data
  • 755 for directories and 644 are set for files
  • Both dockers are in same IP
  • Both websites have their own database, nginx, php hosted on separate dockers

wp-config.php for blog website has following:

/** upload directory to be outside wp-content */
define( 'UPLOADS', '/media/blog/files' ); // where docker is mounted

/** new name for wp-content */
define( 'WP_CONTENT_DIR', '/media/blog/data' ); // where docker is mounted
define( 'WP_CONTENT_URL', '' );

I already added the following code to blog functions.php

if (defined('UPLOADS')) {
    if (is_dir(UPLOADS)) {
        function files_dir($upload_dir){ 

            $upload_dir['path']    = str_replace( $upload_dir['basedir'], UPLOADS, $upload_dir['path'] );
            $upload_dir['basedir'] = UPLOADS;
            return $upload_dir;
        }
        add_filter('upload_dir', 'files_dir');
        } else {
        error_log('UPLOADS is defined but does not exist: ' . UPLOADS);
    }
}

In summary: I just not sure how can I have .jpg as attached image path in blog edit media page rather than: //media/blog/files/image.jpg (there is no typo here // is what I get)

ps.0: .jpg is accessible if I manually type it into address bar, but it is not the URL WordPress (blog) thinks images are, so I end up to have broken images on WordPress unless I manually update them

I managed to upload images out of where blog (WordPress) is installed, and I'm wondering to see how can I update default attached image path.

Few facts:

  • The main website is installed on docker. main
  • There is another stand alone WordPress website (blog) installed on docker: main/blog
  • There is a subdomain on separate docker where I keep blog/wp-content: media.main
  • UPLOAD directory for blog is out of wp-content directory and I can SSH to it by going to where the subdomain docker is: Docker/doc/media.main/blog/files
  • wp-content for blog is renamed to data
  • 755 for directories and 644 are set for files
  • Both dockers are in same IP
  • Both websites have their own database, nginx, php hosted on separate dockers

wp-config.php for blog website has following:

/** upload directory to be outside wp-content */
define( 'UPLOADS', '/media/blog/files' ); // where docker is mounted

/** new name for wp-content */
define( 'WP_CONTENT_DIR', '/media/blog/data' ); // where docker is mounted
define( 'WP_CONTENT_URL', 'https://media.main/blog/data' );

I already added the following code to blog functions.php

if (defined('UPLOADS')) {
    if (is_dir(UPLOADS)) {
        function files_dir($upload_dir){ 

            $upload_dir['path']    = str_replace( $upload_dir['basedir'], UPLOADS, $upload_dir['path'] );
            $upload_dir['basedir'] = UPLOADS;
            return $upload_dir;
        }
        add_filter('upload_dir', 'files_dir');
        } else {
        error_log('UPLOADS is defined but does not exist: ' . UPLOADS);
    }
}

In summary: I just not sure how can I have https://media.main/blog/files/image.jpg as attached image path in blog edit media page rather than: https://main/blog//media/blog/files/image.jpg (there is no typo here // is what I get)

ps.0: https://media.main/blog/files/image.jpg is accessible if I manually type it into address bar, but it is not the URL WordPress (blog) thinks images are, so I end up to have broken images on WordPress unless I manually update them

Share Improve this question edited Nov 27, 2019 at 8:45 clayRay 1351 silver badge10 bronze badges asked Nov 27, 2019 at 0:37 NikNik 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 1

I would try changing your line in wp-config.php to

define( 'UPLOADS', 'media/blog/files' ); // where docker is mounted

Ie, get rid of the first /.

Let me know if this works.

EDIT

If you can't change the definition of UPLOADS you could strip out the forward slash out with code. Try changing your code to this...

$upload_dir['path'] = str_replace( $upload_dir['basedir'], ltrim( UPLOADS, '/'), upload_dir['path'] );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744957640a4603291.html

相关推荐

  • php - Custom location for attached images

    I managed to upload images out of where blog (WordPress) is installed, and I'm wondering to see how can I update de

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信