admin管理员组

文章数量:1296897

The code on the page looks like this:

if ( ! empty( $image ) ) : ?>
<div class="headshot">
    <img src="<?php echo esc_url( $image ); ?>" alt="Photo of <?php echo esc_attr( $author_name ); ?>"/>
</div>

But when the image shows up on the page it's using http instead of https like the rest of the site.

When go to the WP codex to see the usage for esc_url it shows me this under usage but I don't know the correct syntax to use to make the protocol forced to https:

<?php esc_url( $url, $protocols, $_context ); ?>

The code on the page looks like this:

if ( ! empty( $image ) ) : ?>
<div class="headshot">
    <img src="<?php echo esc_url( $image ); ?>" alt="Photo of <?php echo esc_attr( $author_name ); ?>"/>
</div>

But when the image shows up on the page it's using http instead of https like the rest of the site.

When go to the WP codex to see the usage for esc_url it shows me this under usage but I don't know the correct syntax to use to make the protocol forced to https:

<?php esc_url( $url, $protocols, $_context ); ?>
Share Improve this question asked Jun 10, 2015 at 21:55 jimmyplaysdrumsjimmyplaysdrums 1131 silver badge7 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 2

esc_url never force the URL to protocol it's only checking and escaping the passed URL from invalid characters, the $protocols variable is an array of acceptable (white list) protocols not for forcing destination URL.

// Forcing URL to https instead of http

$YOUR_URL = esc_url($YOUR_URL);

if( 'http' == parse_url($YOUR_URL, PHP_URL_SCHEME) ){
    $NEW_URL = str_replace('http://', 'https://', $YOUR_URL); 
}

The $protocols parameter accepts an array, so:

$protocols = array(
  'https'
);

But "https" is listed as a default so I am not sure that is the problem. My guess is that there is a filter interfering... clean_url for example, or kses_allowed_protocols

Old thread but i had similar issue today. If you install SSL Insecure Content Fixer plugin, then go on setting once installed and select 'Capture All' and save.

This fixed the issue for me. Make sure you back up your database and site beforehand just incase something goes wrong.

The function you were looking for (but didn't land until dec 2015), is set_url_scheme

set_url_scheme( $url, 'https' ) )

Internally the function checks if is_ssl is true, will only set the url scheme if the passed url has a scheme.

本文标签: phpescurl( ) won39t use https