How to Create a Sitemap With and Without plugin in WordPress(XML and HTML)?

What is a sitemap?

A sitemap provide information about the posts, pages and other content on your site are intended to help visitors find specific pages, and can 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 show you how to create XML sitemap need to describes the XML schema for the Sitemap protocol. The Sitemap must:

  1. Begin with an opening <urlset> tag and end with a closing </urlset> tag.
  2. Specify the namespace (protocol standard) within the <urlset> tag.
  3. Include a <url> entry for each URL, as a parent XML tag.
  4. 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 functons.php file from theme which located on folder (wp-content/themes/your_theme_folder/functions.php).
Step 2: Below code paste into 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 functons.php file from theme which located on folder (wp-content/themes/your_theme_folder/functions.php).

Step 2: Below code paste into functions.php file: Here i created a shortcode to show the listing. add_shortcode() is WordPress built-in function for shortcode tag. This function requires two parameter. First your shortcode and second name of your php function during execution. You can use your shortcode on 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 or edit page or post form your wp-admin panel and place the shortcode [your_shortcode_name] (here the shortcode name is rsxh_sitemap ) in 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 use a shortcode or echo a shortcode on your template file.

It also generate a XML sitemap which will place on your site root directory (file name sitemap.xml). XML sitemaps will help search engines like Google, Yahoo and Ask.com to better index your site.

Please read details and download Really Simple XML and HTML Sitemap Plugin from the WordPress directory.

4 thoughts on “How to Create a Sitemap With and Without plugin in WordPress(XML and HTML)?

Leave a Reply

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