How to create a Sitemap with and without plugin in WordPress(XML and HTML)?

What is a Sitemap?

A sitemap provides information about the posts, pages and other content on your site are intended to help visitors find specific pages, posts and other information about the website and also be used by crawlers.

There are a couple of different types of sitemaps, such as

  • Visual one that you can create before building a website by its designers.
  • Human visible listings, typically hierarchical, of the pages on a site(HTML).
  • Structured listings intended for web crawlers such as search engines(XML).

***Please scroll down to see complete code block***

How to create XML Sitemap

Before showing you how to create XML sitemap, I need to describes the XML schema for the Sitemap protocol. The Sitemap must:

  • Begin with an opening <urlset> tag and end with a closing </urlset> tag.
  • Specify the namespace (protocol standard) within the <urlset> tag.
  • Include a <url> entry for each URL, as a parent XML tag.
  • Include a <loc> child entry for each <url> parent tag.

All other tags are optional. Optional tags are <lastmod>,<changefreq> and <priority>.

Sample XML Sitemap

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
   <url>
	  <loc>https://wpartisan.net/contact-us/</loc>
	  <lastmod>2020-01-15</lastmod>
	  <changefreq>monthly</changefreq>
	  <priority>0.8</priority>
   </url>
</urlset>

Some Important information about XML tags:

<loc> : This value must be less than 2,048 characters.

<lastmod> : The date of last modification of the file. This date should be in W3C Datetime format. This format allows you to omit the time portion, if desired, and use YYYY-MM-DD.

<changefreq> : Valid values are:

  • always
  • hourly
  • daily
  • weekly
  • monthly
  • yearly
  • never

<priority> : Valid values range from 0.0 to 1.0.

Please check here what sitemaps.org has to say about sitemaps.

Here is the complete code to generate XML Sitemap

Step 1: Open the functons.php file from the theme located in the folder (wp-content/themes/your_theme_folder/functions.php).
Step 2: Copy and paste the below code into the functions.php file:

add_action( 'init', 'wpartisan_xml_sitemap_initialization' );

function wpartisan_xml_sitemap_initialization(){
	global $all_post_type_names;
	$default_posts_types = array(
		'page' => 'page',
		'post' => 'post',
	);
	$args = array(
		'public'   => true,
		'_builtin' => false,
	);
	$custom_post_types = get_post_types( $args, 'names' );
	$all_post_types = array_merge( $default_posts_types, $custom_post_types );
	$all_post_type_names = array_keys( $all_post_types );
	if( !empty( $all_post_type_names ) ) {
		foreach( $all_post_type_names as $post_type_name ):
			add_action( 'publish_'.$post_type_name, 'xml_sitemap' );
		endforeach;
	}
}
function xml_sitemap() {
	global $all_post_type_names;
	$sitemap = '<?xml version="1.0" encoding="UTF-8"?>';
	$sitemap .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
	foreach ( $all_post_type_names as $pty_name ) :
		if( $pty_name === 'page' ):
			$args = array(
				'sort_order' => 'desc',
				'sort_column' => 'post_modified',
				'hierarchical' => 1,
				'exclude' => '',
				'include' => '',
				'meta_key' => '',
				'meta_value' => '',
				'authors' => '',
				'child_of' => 0,
				'parent' => -1,
				'exclude_tree' => '',
				'number' => '',
				'offset' => 0,
				'post_type' => ''.$pty_name.'',
				'post_status' => 'publish'
			); 
			$postsForSitemap = get_pages($args); 
		else:
			$args = array(
				'numberposts' => -1,
				'orderby'     => 'modified',
				'order'       => 'DESC',
				'exclude'     => '',
				'post_type'   => ''.$pty_name.'',
			);
			$postsForSitemap = get_posts($args); 
		endif;

		if( !empty( $postsForSitemap ) ){
			foreach($postsForSitemap as $post) {
				setup_postdata($post);
				$postdate = explode(" ", $post->post_modified);
				$sitemap .= '<url>'.
					'<loc>'. get_permalink($post->ID) .'</loc>'.
					'<lastmod>'. $postdate[0] .'</lastmod>'.
					'<changefreq>monthly</changefreq>'.
				'</url>';
			}
		}
	endforeach;

	$sitemap .= '</urlset>';

	$fp = fopen(ABSPATH . "sitemap.xml", 'w');
	fwrite($fp, $sitemap);
	fclose($fp);
}

How to create HTML Sitemap

Human visible listings, typically hierarchical, of the pages on a site(HTML).

Step 1: Open the functons.php file from the theme located in the folder (wp-content/themes/your_theme_folder/functions.php).

Step 2: Copy and paste the below code into the functions.php file: Here I created a shortcode to show the listing. add_shortcode() is WordPress built-in function for shortcode tags. This function requires two parameters. First is your shortcode, and the second is your PHP function name which will generate the output. You can use your shortcode on a page or post like [your_shortcode_name].

add_shortcode( 'rsxh_sitemap', 'rsxh_sitemap_func' );

function rsxh_sitemap_func( $atts ) {
	$default_posts_types = array(
		'page' => 'page',
		'post' => 'post',
	);
	$args = array(
		'public'   => true,
		'_builtin' => false,
	);
	$custom_post_types = get_post_types( $args, 'names' );
	$all_post_types = array_merge( $default_posts_types, $custom_post_types );
	$all_post_type_names = array_keys( $all_post_types );
	$post_types = implode( ',', $all_post_type_names );
	$params = shortcode_atts( array(
		'post_types' => $post_types,
	), $atts );
	ob_start();
	if( !empty( $params ) ):
		$output = '';
		if( !empty( $params['post_types'] ) ):
			$post_types = explode( ',', $params['post_types'] );
			foreach ( $post_types as $pty_name ) :
				if( $pty_name === 'page' ):
					$output .= wp_list_pages();
				else:
					$args = array(
						'numberposts' => -1,
						'orderby'     => 'modified',
						'order'       => 'DESC',
						'exclude'     => '',
						'post_type'   => ''.$pty_name.'',
					);
					$postsForSitemap = get_posts($args); 

					if( !empty(	$postsForSitemap ) ){
						$output .= '<li class="'.$pty_name.'nav">'.ucfirst($pty_name);
						$output .= '<ul class="'.$pty_name.'">';
				
						foreach($postsForSitemap as $post) {
							setup_postdata($post);
							
							$output .= '<li><a href="'.get_permalink($post->ID).'">'.get_the_title($post->ID).'</a></li>';
						}
						$output .= '</ul>';
						$output .= '</li>';
					}
				endif;
			endforeach;
		endif;
	endif;
	$output .= ob_get_clean();
	return $output;
}

Step 3 : Add shortcode to any pages or posts from wp-admin.

In the last step, you have to create/edit a page or post from your wp-admin panel and place the shortcode [your_shortcode_name] (here, the shortcode name is rsxh_sitemap ) in the content area section and save.

Create a Sitemap With Plugin

Really Simple XML and HTML Sitemap Plugin for WordPress

This WordPress Plugin has a really easy way to add Sitemap on your pages, posts etc. To show HTML Sitemap on any pages or posts, just add a shortcode from add/edit screen or echo a shortcode on your template file.

It also generates an XML sitemap which will be created/placed on your site root directory (file name sitemap.xml). XML sitemaps will help search engines like Google, Yahoo and Ask.com to index your site better.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top