最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Change WordPress upload path and URL

programmeradmin3浏览0评论

On a fresh install, I want to move the upload folder to a subdomain (supposed to speed up download). My subdomain links to a folder called static. So I have:

  • Home
    • wp
      • wp-admin
      • wp-content
      • wp-include
    • static

Now I need to tell WordPress where the upload folder is and define its URL. The codex says I should edit wp-config to define UPLOADS relative to ABSPAHT. But if I put define( 'UPLOADS', '../static' ); of course URL in pages are like //mydomain.tld/wp/../static/image.jpg

I've looked around, and found many different answers to that (filters, DB edit,...), some of them no longer true (since the media settings page no longer allows to change the upload folder) and some obviously wrong... I want to do it the right way.

I went to the wp-admin/options.php page and set upload_path = ../static and upload_url_path = and that seems to work.

But is that how it's supposed to be done? And if developpers have removed these options from the media settings page, isn't there a risk that the feature is later completely removed?

On a fresh install, I want to move the upload folder to a subdomain (supposed to speed up download). My subdomain links to a folder called static. So I have:

  • Home
    • wp
      • wp-admin
      • wp-content
      • wp-include
    • static

Now I need to tell WordPress where the upload folder is and define its URL. The codex says I should edit wp-config to define UPLOADS relative to ABSPAHT. But if I put define( 'UPLOADS', '../static' ); of course URL in pages are like //mydomain.tld/wp/../static/image.jpg

I've looked around, and found many different answers to that (filters, DB edit,...), some of them no longer true (since the media settings page no longer allows to change the upload folder) and some obviously wrong... I want to do it the right way.

I went to the wp-admin/options.php page and set upload_path = ../static and upload_url_path = http://static.mydomain.tld and that seems to work.

But is that how it's supposed to be done? And if developpers have removed these options from the media settings page, isn't there a risk that the feature is later completely removed?

Share Improve this question asked Apr 3, 2016 at 16:31 MatMat 1891 gold badge3 silver badges9 bronze badges 2
  • Whatever the codex says is the correct way of doing things. If that doesn't suit your needs then whatever solution you can find is a workaround, and there are no right or wrong workarounds. So if it works for you for now, stick to it and just watch for changes/updates. – ed-ta Commented Apr 3, 2016 at 16:41
  • 1 Sure, but that codex page only deals with upload_path, not upload_URL. And that page is only about editing wp-config, plus the whole section has a disclaimer "The following sections may contain advanced / unsupported information" which makes me wonder. – Mat Commented Apr 3, 2016 at 16:50
Add a comment  | 

3 Answers 3

Reset to default 9

I went to the wp-admin/options.php page and set ... But is that how it's supposed to be done?

Nope. You should never change anything in the WordPress core files because all your changes will be lost during the next update. You should use actions and filters instead:

add_filter( 'pre_option_upload_path', function( $upload_path ) {
    return '/path/to/static';
});

add_filter( 'pre_option_upload_url_path', function( $upload_url_path ) {
    return 'http://static.example';
});

I had a similar problem mapping subdomain media. Asked & Answered here.
In short, add to functions.php the following:

update_option('upload_url_path', '/wp-content/uploads');

If, for whatever reason you don't want to set the upload path options in the database or in your functions.php, you can still set the option filters in your wp-config.php like this (before calling any WordPress core file):

$GLOBALS['wp_filter']['pre_option_upload_path'][10][] = array(
  'function' => function( $upload_path ){
    return '/path/to/static';
  },
  'accepted_args' => 1,
);

$GLOBALS['wp_filter']['pre_option_upload_url_path'][10][] = array(
  'function' => function( $upload_url_path ){
    return 'http://static.example';
  },
  'accepted_args' => 1,
);

WP_Hook::build_preinitialized_hooks will properly update these array items to the actually used internal format.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论