If you are in need of rewriting HTTP URLs stored in your WordPress database installation and don’t want to manually change them into HTTPs this little trick can be useful to you.
In my case, I needed to change all the URLs of the attachments of posts like this “http://mydomain.com/files/2012/11/image01.jpg” to HTTPS. If there were a simple installation I would manually changed it by hand in the database but these case envolves a Multisite WordPress with hundreds of blogs and multiple posts.
How did I solve it? WordPress Filters came to the rescue. This is how I solve it in the “functions.php” of the theme:
function changeToHTTPS($url) {
$return = str_replace('http://mydomain.com', 'https://mydomain.com', $url);
return $return;
}
if (defined('FORCE_SSL_LOGIN') && FORCE_SSL_LOGIN) {
add_filter('wp_get_attachment_url', 'changeToHTTPS');
}