Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

My site requires the use of https for all img src HTML.

This is the WordPress function I'm using to display images:

<img src="'.get_bloginfo("template_url").'/images/thumb-default.gif" />

This outputs an http img src - how can I convert that to https?

share|improve this question

4 Answers 4

up vote 6 down vote accepted

WordPress checks the return value of is_ssl() before creating URLs using get_bloginfo(). If the function returns true, it creates https URLs. If it returns false, it creates http URLs.

From the WordPress source ...

function is_ssl() {
    if ( isset($_SERVER['HTTPS']) ) {
        if ( 'on' == strtolower($_SERVER['HTTPS']) )
            return true;
        if ( '1' == $_SERVER['HTTPS'] )
            return true;
    } elseif ( isset($_SERVER['SERVER_PORT']) && ( '443' == $_SERVER['SERVER_PORT'] ) ) {
        return true;
    }
    return false;
}

So ... if the request was made over https or if the request came in over port 443, then get_bloginfo() will return an https:// URL. In reality, if you're forcing https anyway, you should force all requests to port 80 (http) over to port 443 (https) ... but that's a server configuration issue, not a WordPress issue.

Alternatively, you could hook into a filter and replace http with https ...

Just use:

function replace_http( $original ) {
    // use preg_replace() to replace http:// with https://
    $output = preg_replace( "^http:", "https:", $original );
    return $output;
}

add_filter( 'template_url', 'replace_http' );
share|improve this answer
    
I get a parse error when I add that code to my functions.php file. The $ouput line is also marked red in Dreamweaver. –  Ryan Apr 18 '11 at 18:16
    
I didn't give you the full code ... you have to actually put something inside the preg_replace() code, and I can't give you the regex at the moment. Replace preg_replace( ... ); with the actual call and you should be fine. –  EAMann Apr 18 '11 at 18:20
    
I'm not sure what to write. If you have time to provide the regex/full code I would greatly appreciate it! –  Ryan Apr 18 '11 at 18:22
    
Updated with regex code. –  EAMann Apr 18 '11 at 18:30

Had you checked this when requesting page via http or https link? What does is_ssl() return for you?

I don't have SSL-capable stack to test at moment, but I am pretty sure that WP functions that put together links should be aware of SSL and output correct version.

share|improve this answer
    
The page URL in my browser is https, but the img src links are http. –  Ryan Apr 18 '11 at 18:11
    
@Ryan this is not normal behavior, I've checked and it should be https link generated in such case... see EAMann's answer for extended details. –  Rarst Apr 18 '11 at 18:13

Using the home_url() function will automatically detect ssl and change protocols.

share|improve this answer

To add to the fix proposed by EAMann I have made some changes for version 3.5:

add_filter( 'template_directory_uri', function( $original ) {
    $output = preg_replace( "/^http:/i", "https:", $original );
    return $output;
});

I'd like to note that this redirect thing may be very helpful, but my server redirects SSL to another port and is not detected by WP so for me it's an annoyance.

share|improve this answer
    
Are you referring to WordPress 3.5? Is so, that's great! –  Ryan Apr 17 '13 at 17:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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