6

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?

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

3 Answers 3

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.org';
});
8
  • 2
    No, no, there's a misunderstanding here. Of course I would never edit a WordPress file. I meant I loaded the wp-admin/options.php page in my browser, changed the settings displayed and clicked save. Now, for your filters, I understand how they work, but where are the values obtained from without filter? Any chance they are the settings I modified?
    – Mat
    Commented Apr 3, 2016 at 17:08
  • Gotcha, sorry for assuming you edited a core file. Those are exactly the values you modified and the pre_option_* filter simply filters all get_option() calls for those values, so your approach will also work. Re. future-proof, I doubt these options will ever be removed from core without providing backwards compatibility.
    – kovshenin
    Commented Apr 3, 2016 at 17:20
  • OK, so basically both our solutions are similar in a sense that mine defines new option values (in database I assume) while yours change them on the fly, right? So I guess this, together with your comment regarding future evolutions mean my solution is correct. Thanks for your help.
    – Mat
    Commented Apr 3, 2016 at 17:31
  • 1
    You're welcome! Right, but just to clarify, both values are already defined in the database, you're simply changing them from the options.php UI. I would also not use relative paths because they're sometimes unpredictable.
    – kovshenin
    Commented Apr 3, 2016 at 17:38
  • Just checked in two other installs I have. Both upload_path and upload_url_path exist in DB but are empty, so I'm assuming a default is defined somewhere. As for relative path that was because of the hint from the codex I mentionned in my initial post. I will set an absolute path then, hopefully that won't break my site :-)
    – Mat
    Commented Apr 3, 2016 at 17:46
0

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');
0

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.org';
  },
  'accepted_args' => 1,
);

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.