2

If anybody have a idea about codeigniter library for updating the sitemap.xml file through codeinginter? Tried to follow this tutorial but not know what file to create and where :https://github.com/chemicaloliver/codeigniter-sitemaps. I will appreciate any help.

Thanks.

1 Answers1

0

Simply put the library file in application/libraries folder with name sitemaps and add sitemaps config file in config folder. Then load library in your controller as

             $this->load->library('sitemaps');

and in your controller method do

    function add()
    {
      $item = array(
        "loc" => site_url("page/" . "your title of page"),
        "lastmod" => date("c", strtotime('2017-01-01')),
        "changefreq" => "daily",
        "priority" => "0.8"
    );

    $this->sitemaps->add_item($item);
    // file name may change due to compression
    $file_name = $this->sitemaps->build("sitemap.xml");
    $this->sitemaps->ping(site_url($file_name));

  }

Then simply modify your libraru only to keep xml file and update it. So modify build function of library file as

             function build($file_name = null, $gzip = NULL)
{
     $map = $this->CI->config->item('sitemaps_header') . "\n";
    $xml=simplexml_load_file($file_name);
    foreach($xml->children() as $books) {
    $books['loc'] = htmlentities($books['loc'], ENT_QUOTES);
        $map .= "\t<url>\n\t\t<loc>" . $books->loc . "</loc>\n";
        $map .= "\t\t<lastmod>" . $books->lastmod . "</lastmod>\n";
        $map .= "\t\t<changefreq>" . $books->changefreq . "</changefreq>\n";
        $map .= "\t\t<priority>" . $books->priority . "</priority>\n";
        $map .= "\t</url>\n\n";
       } 


    foreach ($this->items as $item)
    {
        $item['loc'] = htmlentities($item['loc'], ENT_QUOTES);
        $map .= "\t<url>\n\t\t<loc>" . $item['loc'] . "</loc>\n";
        $attributes = array("lastmod", "changefreq", "priority");
        foreach ($attributes AS $attr)
        {
            if (isset($item[$attr]))
            {
                $map .= "\t\t<$attr>" . $item[$attr] . "</$attr>\n";
            }
        }

        $map .= "\t</url>\n\n";
    }
    //unset($this->items);
    $map .= $this->CI->config->item('sitemaps_footer');
    if (!is_null($file_name))
    {
        $fh = fopen($file_name, 'w');
        if (!$fh)
        {
            $this->set_error('Could not open sitemaps file for writing: ' . $file_name);
            return FALSE;
        }
        if (!fwrite($fh, $map))
        {
            $this->set_error('Error writing to sitemaps file: ' . $file_name);
            return FALSE;
        }
        fclose($fh);
        if ($this->CI->config->item('sitemaps_filesize_error') && filesize($file_name) > 1024 * 1024 * 10)
        {
            $this->set_error('Your sitemap is bigger than 10MB, most search engines will not accept it.');
            return FALSE;
        }
        return $file_name;
    }
    else
    {
        return $map;
    }
}
Homnath Bagale
  • 464
  • 1
  • 6
  • 32