PHP Code Generating Free XML Sitemap for WordPress Site (without plugin)

  1. Previous
  2. Next
  3. Recent

    1. Signs 'He' May be Cheating on You
    2. 7 Vows of Hindu Marriage - Know What 'Saat Vachans' Mean
    3. RuPay vs Visa vs Mastercard vs American Express- A Comparison
    4. What is RuPay? Everything You Need to Know About India's Domestic Payment System!
    5. Top 10 Online Portals to Find a High Paying Job in India
    6. Top Indians who Revolutionized the IT Sector in India
    7. 8 Initiatives by Narendra Modi that Could Change the Future of the Nation
    8. 8 Work-from-home Jobs for Housewives, Students, and Part timers
    9. Complete Company Registration Process in India - Explained
    10. Change Name in India in 5 Steps - Filing Affidavit, Newspaper Ad & Gazette Notification

Sitemap is a collection(or a map) of all links present in a website. For a WordPress site, its a collection of published posts, pages and images contained in them.

PHP Code to Create Sitemap for WordPress for free

There is no service that provides free sitemap creation except for xml-sitemap which is limited to 500 urls.

Using PHP, we can create a free xml sitemap in minutes . All we have to do is to fetch all weblinks from database and save to a file in XML format defined by and complied by all search engines.

Here is a sample PHP code generating free xml sitemap for WordPress site.

makeDbConnection( 'wp_host', 'wp_username', 'wp_password', 'wp_database_name' ); // Database Connection
$published_posts = getPublishedPosts(); // Get a list of published post links
$sitemapInfo = generateFreeXMLSitemap( $published_posts ); // Create Sitemap

1. makeDbConnection makes a connection to the database of your WordPress site.

Replace wp_host with your database host (most likely ‘localhost’), wp_username, wp_password and wp_database_name with username, password and database name of your WordPress site respectively.

You’ll find all these in your wp-config.php file of WordPress folder.

// Make Database Connection
function makeDbConnection( $host, $username, $password, $dbname ) {
	$con = mysql_connect( $host, $username, $password );
	$selectdb = mysql_select_db( $dbname, $con );
	mysql_set_charset( 'utf8' );
	if( !$con && !$selectdb ) {

2. getPublishedPosts gets a list of all published posts to be included in the sitemap. The function can also be used to get a list of pages and images with slight change of code (Explained Below).

// Get Published Post Links
function getPublishedPosts() {
	$q = mysql_query("SELECT * FROM wp_posts WHERE post_status='publish' AND post_type='post'");
	// For pages
	// $q = mysql_query("SELECT * FROM wp_posts WHERE post_status='publish' AND post_type='page'");
	// For images
	// $q = mysql_query("SELECT * FROM wp_posts WHERE post_status='inherit' AND post_type='attachment' AND post_mime_type like '%image%'");
	$posts_url = array();
	while( $r = mysql_fetch_assoc( $q ) ) {
		array_push( $posts_url, $r['guid'] );
	return $posts_url;

3. generateFreeXMLSitemap creates our free sitemap in xml format.

// Create Free XML Sitemap
function generateFreeXMLSitemap( $results ) {
	$smap = '<?xml version="1.0" encoding="UTF-8"?>'; // XML Decaration
	$smap .= "\r\n".'<urlset
	$smap .= "\r\n"."<!-- Generating Free XML Sitemap for WordPress Blog -->";
	foreach( $results as $p_url ) {
		static $count_listed_posts_url = 0;
		$smap .= "\r\n<url>";
				$smap .= "\r\n\t<loc> $p_url </loc>";
		$smap .= "\r\n</url>";
	$smap .= "\r\n</urlset>";
	$sitemap_file = fopen( 'sitemap.xml', 'w' ); // Create XML file to include sitemap info
	$written_to_sitemap = fwrite( $sitemap_file, $smap );
	fclose( $sitemap_file );
	return array( 'status_listed_posts_url' => $written_to_sitemap, 'count_listed_posts_url' => $count_listed_posts_url );

That’s it. Just copy and paste the above code anywhere on your web-server and run. Your free xml sitemap will be created besides the php file ie in the same folder.


Next goto Google webmasters and upload sitemap and Google will index all links within 24 hours.

Why Sitemap Submission is important for Websites?

By submitting sitemap, you tell search engines like Google, Bing and Yahoo that my website has these content and index them asap so that it starts appearing in search results immediately.

Submitting sitemap isn’t compulsory. But there are instances where a page remain unindexed for days and months. This happens mainly with new sites or with sites publishing content in days and weeks. This may also happen when, due to some reason, search engines fail to find your website content. To avoid such situation, its always advisable to submit sitemap to search engines on regular basis.

blog comments powered by Disqus