2

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( '/' ) ) . '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( '/' ) ) . '</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 );
}
}
  • I was thinking about str_replace but it will takes so much memory since we are talking about thousands and thousands of links – Chymmi Sep 4 '16 at 16:20
  • does get_permalink output a https link when used on the other pages ? – mmm Sep 4 '16 at 16:42
  • Is the generated URL otherwise correct, that is the only difference between expected result and what you get is the lack of https://? – Mateusz Hajdziony Sep 4 '16 at 17:35
  • yes on other pages like (category.php/single.php) it will generate link with https ...and yes that url is correct only difference is that is without https :( – Chymmi Sep 4 '16 at 18:10
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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