Search code examples
wordpressmultisite

Wordpress - uploading images to one domain but URL should be a different domain


I'm trying to get my head around this. I know Wordpress pretty well but I'm officially confused.

I have a subdomain where users can login to wordpress at dev.mysite.com/wp-admin. When they upload images to dev.mysite.com/wp-content/images/uploads, those files are lsynced to www.mysite.com/wp-content/uploads. I changed wp-config.php so that it will work on either domain.

This works okay except that in wordpress posts, when you upload an image, it uploads the domain as well so it puts in html like and my users have to go in and change dev. to www. manually since "dev." is password protected so the images won't even show up just a password prompt if they forget to do that.

Is there a way to make it so wordpress doesn't include the full path to the image? Where is that in wordpress' guts?


Solution

  • Add this to your themes functions.php:

    add_filter( 'wp_get_attachment_url', 'wp_update_attachment_url_domain', 10, 2 );
    
    function wp_update_attachment_url_domain ($url, $post_id)
    {
       $url = str_ireplace('dev.mysite.com', 'www.mysite.com', $url);
    
       return $url;
    }
    

    This will updated the url of the image by replacing the host name.