Skip to content

dipankarmaikap/wp-sitemap-rest-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WP Sitemap Rest API Plugin

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

Quick Install

  • Clone or download the zip of this repository into your WordPress plugin directory & activate the WP Sitemap Rest Api plugin

Find this useful?

Buy Me A Coffee

Usage

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.

Get total number of posts, pages, authors, tags and categories.

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
}

Get author urls

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',
  }
]

Get taxonomy urls

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',
  }
]

Get post or page urls

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

Contributions are welcome :). This was a very quick build. Feel free to make a PR against this repo!

Open an issue

@maikap_dipankar

About

This plugin adds REST API end points for generating sitemap for your headless wordpress site.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages