How to create Wordpress Sitemap Without Plugin using php with simplest way? Here is the tips.
What we need?
First wordpress website mus have a rss. and fortunately, its built in wordpress pack. Example, the rss link go to https://izulthea.com/feed. With this rss, we will extract permalink and date, both data used in sitemap correct format.
Sitemap Correct Format
According to sitemaps.org, correct format is:
<?xml version=”1.0″ encoding=”UTF-8″?>
<urlset xmlns=”http://www.sitemaps.org/schemas/sitemap/0.9″>
<url>
<loc>http://www.example.com/</loc>
<lastmod>2005-01-01</lastmod>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>
</urlset>
Extract Link and Date
Now, we prepare a php script to extract that rss, to get link and date.
$rss = new DOMDocument(); $rss->load('http://izulthea.com/feed'); $feed = array(); foreach ($rss->getElementsByTagName('item') as $node) { $item = array ( 'title' => $node->getElementsByTagName('title')->item(0)->nodeValue, 'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue, 'link' => $node->getElementsByTagName('link')->item(0)->nodeValue, 'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue, ); array_push($feed, $item); } $limit = 10; for($x=0;$x<$limit;$x++) { //$title = str_replace(' & ', ' & ', $feed[$x]['title']); $link = $feed[$x]['link']; // $description = $feed[$x]['desc']; $date = date('Y-m-d', strtotime($feed[$x]['date'])); }
Above scipt will extract title, link, description and date. But we only need link and date.
Put all together
<?php
header(“Content-type: text/xml”);
echo ‘<?xml version=”1.0″ encoding=”UTF-8″?>’;
echo ‘<urlset xmlns=”http://www.sitemaps.org/schemas/sitemap/0.9″>’;
///////////
$rss = new DOMDocument();
$rss->load(‘http://izulthea.com/news/makefulltextfeed.php?url=https://izulthea.com/feed&max=500&links=preserve&exc=1&submit=Create+Feed’);$feed = array();
foreach ($rss->getElementsByTagName(‘item’) as $node) {
$item = array (
‘title’ => $node->getElementsByTagName(‘title’)->item(0)->nodeValue,
‘desc’ => $node->getElementsByTagName(‘description’)->item(0)->nodeValue,
‘link’ => $node->getElementsByTagName(‘link’)->item(0)->nodeValue,
‘date’ => $node->getElementsByTagName(‘pubDate’)->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 10;
for($x=0;$x<$limit;$x++) {
//$title = str_replace(‘ & ‘, ‘ & ‘, $feed[$x][‘title’]);
$link = $feed[$x][‘link’];
// $description = $feed[$x][‘desc’];
$date = date(‘Y-m-d’, strtotime($feed[$x][‘date’]));echo ‘<url>’;
echo ‘<loc>’.$link.'</loc>’;
echo ‘<lastmod>’.$date.'</lastmod>’;
echo ‘<changefreq>weekly</changefreq>’;
echo ‘ <priority>0.8</priority>’;
echo ‘</url>’;
///////////
}
echo ‘</urlset>’;
?>
Validate sitemap
To validate sitemap just go to https://www.xml-sitemaps.com/validate-xml-sitemap.html, paste sitemap link there.
That`s all simple step to create sitemap without plugin. You may also create sitemap without plugin by adding some code to functions.php.
Submit site map to goole:
google.com/ping?sitemap=https://izulthea.com/sitemap.php