Having admin on different host breaks attached images

Sometimes it’s nice to have admin on a different host (admin.mysite.com) then the actual site (www.mysite.com).

If admin.mysite.com is not publicly accessible, then images attached to posts on admin.mysite.com will not be accessible on www.mysite.com since the url to the attached image is absolute and contains the domain (admin.mysite.com).

Is there any way to fix this? The nicest way would be if the url to an attached media would be relative and not absolute.

Solutions Collecting From Web of "Having admin on different host breaks attached images"

you can configure the tinymce to use relative paths for images using the tiny_mce_before_init like this:

function tinymce_relative_path( $init ) {
 $init['relative_urls'] = true;
 $init['document_base_url'] = 'http://www.mysite.com';
 return $init;
add_filter('tiny_mce_before_init', 'tinymce_relative_path');

I found another way of doing this. I added the following to functions.php:

function yoursite_get_relative_attachment_path($path)
    $paths = (object)parse_url($path);
    return $paths->path;

function yoursite_wp_handle_upload($info)
    $info['url'] = yoursite_get_relative_attachment_path($info['url']);
    return $info;
add_filter('wp_handle_upload', 'yoursite_wp_handle_upload');

function yoursite_wp_get_attachment_url($url)
    return yoursite_get_relative_attachment_path($url);
add_filter('wp_get_attachment_url', 'yoursite_wp_get_attachment_url');

This way wordpress stores the relative URL in the db.

For more detailed instructions see the blog where I learned about this.