• Resolved Kajuzi

    (@kajuzi)


    I am using a plugin which moves uploaded files to a remote location and reroutes any requests to those files because in the postmeta table, the meta_value for _wp_attached_file is still the local file location. Even though it’s safer to do it that way, I really don’t like the whole filter-at-load-time business because when the plugin is running, the home page is taking 20s to load.

    Even though I have manually rewritten the values to replace them with the S3 bucket full URLs ( hxxps://s3.amazonaws.com/url/to/image.ext ), in the media library and on the front-end WordPress still tries to find them on the local server ( hxxps://1ocalhost/url/to/image.ext ).
    WordPress seems to be replacing everything before ‘wp-content’ with ‘hxxp://localhost/’. How do I change this behaviour?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    WP saves the media paths to post meta as the media is uploaded. You can alter any related meta data just before it is saved with the filter ‘wp_update_attachment_metadata’.

    FWIW, ‘rewrite’ is a poor choice of words in this context as the term has a very specific meaning in server administration that is unrelated to what you are asking. WP does not rewrite any physical file paths. If a physical file is requested, WP is not even involved, so it cannot rewrite anything. WP determines the default media path from a particular hidden option or defined constant. If neither are defined, the default is constructed from the constant ABSPATH and the value set for the site url option. Determining the media path this way would not be considered a rewrite.

    But I knew what you meant, so it’s all good ??

    Thread Starter Kajuzi

    (@kajuzi)

    I can’t believe you’ve just chosen to correct my semantics and not given a solution ??
    I can’t use wp_update_attachment_metadata() because I’m not uploading these files. They have been uploaded already.
    My question still remains: How do I keep the correct uURL in the database and make sure that the remote file path is not converted to a local one when the file is requested

    Moderator bcworkz

    (@bcworkz)

    I was answering the primary question in the topic title. Sorry I didn’t answer every question in your post. It wasn’t my intention, it just happens. I sometimes fail at good reading comprehension.

    I’m not sure there is a single all encompassing solution. The solution will depend on what specific access is being done. Anyway, if the entire uploads location is at S3, go to the semi-hidden options screen at /wp-admin/options.php. Locate the ‘upload_url_path’ option and set it to the S3 equivalent of your usual httр://example.com/wp-content/uploads local location. There is no valid equivalent you can use for the related ‘upload_path’ option unless you have a local mirror. Anything that depends on full local paths will need to be addressed separately. The URL path option should cover most situations.

    Thread Starter Kajuzi

    (@kajuzi)

    Thank you very much. This is exactly what I wanted.
    Unfortunately, because of some images in some funny locations that are not being uploaded to S3, I can’t use this simple solution

    Moderator bcworkz

    (@bcworkz)

    If code is using get_option() to retrieve what is stored in ‘upload_url_path’, what is returned can be dynamically filtered with the filter ‘option_upload_url_path’. There are frequently other filters for other WP functions that return URLs. It’s a matter of knowing which functions are used in your situation.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How Does WordPress Rewrite Media Files URLs’ is closed to new replies.