Support » Plugin: Andrea Pernici News Sitemap for Google » Adaptation done for WPML – seems to work :)

  • Agnes

    (@agneslesagegmailcom)


    Hello,

    I am using WPML on a website with WPML settings where each post comes with:
    http://www.mysitedomain.com/%5Blg%5D/news/postname
    Where [lg] is the language, either “fr” or “en” in my case.

    I did not find any Google news sitemap plugin a priori compatible for WPML.
    I tried your plugin thinking it would not work…. and found it did work but needs a little adaptation.

    What works:
    posts from all languagues are properly returned from the plugin

    What does not work:
    If I put nothing as a language, Google Webmaster Tools while testing the output XML file tells me the language tag is missing
    If I put both “fr, en” it tells me it is wrong.

    So I did this adaptation in the plugin file apgnsm.php:

    1> I commented the choice of language from the admin

    <?php // WPML custom remove language choice by ALP ?>
    <!--<li><label for="apgnsm_n_lang">Your Article Language (it, en, es...): <input name="apgnsm_n_lang" type="text" id="apgnsm_n_lang" value="<?php echo $apgnsm_n_lang?>" /></label></li>-->

    2> I added a piece of code, at the beginning of the loop, that retrieves the language directory from the post URL

    if ($apgnsm_news_active && $apgnsm_permission != 2) {
        $postDate = strtotime($post->post_date);
        if ($now - $postDate < $twoDays) {
    
    // WPML custom by ALP: retrieve URL & then retrieve language from WPML path
    // that works with path like http://www.mydomain.com/lg/etc where  lg is the WPML language string.
    $posturl = apgnsm_escapexml(get_permalink($post->ID));
    $urlarray = explode("/",$posturl);
    $urllanguage = strtolower($urlarray[3]);
    
    // Go build the sitemap	(end WPML)
    $xml_sitemap_google_news .= "

    NB1: there might be a better way to do that but I am not sure…
    NB2: in the case of a different build of URL, “$urlarray[3]” should be adapted with the right piece in the URL (in my case with http://www.mysitedomain.com/%5Blg%5D/news/postname, 3rd piece separated by “/”)
    The result is: that works fine for me, returning the proper language 🙂
    Like <n:language>en</n:language>

    3> Additionnally, I find it convenient to add a link to the output sitemap here:

    Create news sitemap: <a href="<?php echo site_url(); ?>/sitemap-news.xml"><?php echo site_url(); ?>/sitemap-news.xml</a>.<br /><br />
    NB: the sitemap will be regenerated after you save and rebuild below.

    I put the modified file here if anyone is interested,
    (any addition is marked with “ALP”):
    https://drive.google.com/file/d/0B44c58FUa6gSdWF0ajB2Tmc5emM/edit?usp=sharing

    To make it better, a tick box should be offered in the admin, to choose use that method instead of manual language input, I guess, but I leave it to you if you wish to ijntegrate that addition.

    Thanks Andrea for the great plugin!
    Agnes

    https://wordpress.org/plugins/google-news-sitemap/

  • The topic ‘Adaptation done for WPML – seems to work :)’ is closed to new replies.