get_permalink executes link without https

Currently I am using https version of website and everything is fine, I setupped site main url for https, when viewing editor of post there is https permalink presented..everything works fine on all category pages, homepage etc.

But when I am using custom sitemap function (I created my own plugin for it) it will list sitemap with http:// and not with https://

add_action("publish_post", "eg_create_sitemap");
add_action("publish_page", "eg_create_sitemap");
add_action( "save_post", "eg_create_sitemap" );
function eg_create_sitemap() {
if ( str_replace( '-', '', get_option( 'gmt_offset' ) ) < 10 ) { 
    $tempo = '-0' . str_replace( '-', '', get_option( 'gmt_offset' ) ); 
} else { 
    $tempo = get_option( 'gmt_offset' ); 
}
if( strlen( $tempo ) == 3 ) { $tempo = $tempo . ':00'; }
$postsForSitemap = get_posts( array(
    'numberposts' => -1,
    'orderby'     => 'modified',
    'post_type'   => array( 'post', 'page' ),
    'order'       => 'DESC'
) );
$sitemap .= '<?xml version="1.0" encoding="UTF-8"?>' . '<?xml-stylesheet type="text/xsl" href="' . 
    esc_url( home_url( "https://wordpress.stackexchange.com/" ) ) . 'sitemap.xsl"?>';
$sitemap .= "\n" . '<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
$sitemap .= "\t" . '<url>' . "\n" .
    "\t\t" . '<loc>' . esc_url( home_url( "https://wordpress.stackexchange.com/" ) ) . '</loc>' .
    "\n\t\t" . '<changefreq>daily</changefreq>' .
    "\n\t\t" . '<priority>1.0</priority>' .
    "\n\t" . '</url>' . "\n";
foreach( $postsForSitemap as $post ) {
    setup_postdata( $post);
    $postdate = explode( " ", $post->post_modified );
    $sitemap .= "\t" . '<url>' . "\n" .
        "\t\t" . '<loc>' . get_permalink( $post->ID ) . '</loc>' .
        "\n\t\t" . '<changefreq>Weekly</changefreq>' .
        "\n\t\t" . '<priority>1</priority>' .
        "\n\t" . '</url>' . "\n";
}
$sitemap .= '</urlset>';
$fp = fopen( ABSPATH . "sitemap.xml", 'w' );
fwrite( $fp, $sitemap );
fclose( $fp );
}
}

1 Answer
1

this may be called as a bug in get_permalink, but it may not be.
I had the same case, and when I tried to echo site_url(); I get the https://…

But once I updated the options for siteurl and home to have https

get_option( 'siteurl' );
get_option( 'home' );

get_permalink worked fine.

I noticed that on multisite.

Leave a Comment