This plugin adds REST API end points for generating sitemap for your headless wordpress site.
Using this plugin? I would love to see what you make with it. 😃 @maikap_dipankar
- Clone or download the zip of this repository into your WordPress plugin directory & activate the WP Sitemap Rest Api plugin
This plugin adds 4 end points to wp rest api
Check out my blog post on how you can use this plugin and nextjs to create dunamic sitemap.
import axios from "axios";
export default async function getTotalCounts() {
const res = await axios.get(
`${process.env.NEXT_PUBLIC_WORDPRESS_URL}/wp-json/sitemap/v1/totalpages`
);
return (await res?.data) ?? {};
}
This will then give you a result as such:
{
"category": 1,
"tag": 0,
"post": 15,
"page": 1,
"user": 1,
"any_custom_post_type_you_have":0
}
This gives you two option to add page no and how many items you want in one request.
import axios from "axios";
export default async function getAuthorUrls() {
const res = await axios.get(
`${process.env.NEXT_PUBLIC_WORDPRESS_URL}/wp-json/sitemap/v1/author?pageNo=${page}&perPage=${sitemapPerPage}`
);
return (await res?.data) ?? [];
}
This will give you a result like below:
[
{
"url": '/author/dipankarmaikap',
},
{
"url": '/author/willsmith',
}
]
This gives you two option to add page no and how many items you want in one request like the above + it gives you the option to add the taxonomy type (eg: category or tag)
import axios from "axios";
export default async function getTaxonomyUrls() {
const res = await axios.get(
`${process.env.NEXT_PUBLIC_WORDPRESS_URL}/wp-json/sitemap/v1/taxonomy?pageNo=${page}&taxonomyType=${type}&perPage=${sitemapPerPage}`
);
return (await res?.data) ?? [];
}
This will give you a result like below:
[
{
"url": '/category/news',
},
{
"url": '/category/australia',
}
]
This gives you two option to add page no and how many items you want in one request like the above + it gives you the option to add the post type (eg: page or post)
import axios from "axios";
export default async function getPostsUrls() {
const res = await axios.get(
`${process.env.NEXT_PUBLIC_WORDPRESS_URL}/wp-json/sitemap/v1/posts?pageNo=${page}&postType=${type}&perPage=${sitemapPerPage}`
);
return (await res?.data) ?? [];
}
This will give you a result like below:
[
{
"url": '/blog/hello-world',
"post_modified_date": "14 April 2022"
},
{
"url": '/blog/how-to-create-a-wp-plugin',
"post_modified_date": "14 April 2022"
}
]
Contributions are welcome :). This was a very quick build. Feel free to make a PR against this repo!