How to create sitemap in WordPress without plugin

In our Previous Article we learn what is Sitemap and why sitemap is important. If you are a WordPress user you can generate sitemap very easily with WordPress Plugins. But in this article, I am going to show you how to create HTML and XML sitemap in WordPress without plugin.

As we know that plugin is a considerable feature in WordPress, it adds new functionality to your WordPress site. but excessive use of plugin might become headache for you. It can slow down your site.

10 best tips to speed up WordPress website

HTML sitemap designed for Users, where user can easily view all links, pages category of the site. to create HTML sitemap for your WordPress website. follow below steps.

Step 1: Open functons.php file from theme

In First step you have to do is open your currently active theme from wp-content/theme folder. In that folder you will see many php pages. like header.php, footer.php, functions.php etc. So open functions.php file in your text editior.

step 2: Download below code and paste into functions.php

You can download source code from Github and paste into functions.php add_shortcode(‘sitemap’, ‘html_sitemap’); 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].

Step 3: Add new page from wp-admin

Now in the last step we have to do is create new page form your wp-admin panel and Give appropriate name. In this tutorial am giving page name sitemap and paste shortcode [sitemaps] and save.

Now your site is ready with Dynamic HTML Sitemap. Whenever there is new page or post published, the link of that post/page will automatically listed in sitemap.

How to create XML Sitemap

XML Sitemap generally designed for search engines to crawling and indexing our web pages and to do that first we have to create XML sitemap and we have to submit our XML sitemap to Webmaster Tool. Follow below steps to create XML sitemap.

Step 1: Open functons.php file from theme

In first step again we have to open functions.php file in your text editor from your theme folder (wp-content/themes/your_theme_folder/functions.php).

Step 2: Download below code and paste into functions.php file

Just download and copy-paste code into your functions.php file.

add_action(“publish_post”, “XML_sitemap”); add_action(“publish_page”, “XML_sitemap”);

In downloaded file, you will see above 2 lines in that file. Here, add_action() hooks or call on a specific action. This function takes two parameter, first is the name of the action and second parameter is the name of function you wish to be hook.

For e.g : In above code we pass first parameter publish_post. So whenever you will publish your new post at that time WordPress will call a custom php function XML_sitemap which we passed as second parameter to hook.

Now new page or post published on your site, this code will clear older content and write new content in existing file. Check your sitemap by www.mydomain.com/sitemap.xml, you will see updated XML sitemap of your site.

Thanks for reading my blog.


Originally published at techyygeeks.com on August 6, 2015.

Show your support

Clapping shows how much you appreciated Rohit Gilbile’s story.