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.


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');
  • This doesn't seeem to work for me. The function 'tinymce_relative_path' is being called (from functions.php) but tinymce still doesn't save relative urls. I'm running WordPress 3.1.4. – D. Wroblewski Aug 7 '11 at 15:58

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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