Skip to content

zeroquinze/wordpress-sdk

Repository files navigation

[WIP] Wordpress SDK

npm GitHub Build Status

Built with axios, this package provides a wrapper for Wordpress REST API for fetching data from wordpress hosted sites.

Installation

$ npm install wordpress-sdk --save

or if you use yarn

$ yarn add wordpress-sdk

Usage

Initializing configuration

import { wordpress } from 'wordpress-sdk'
// or
const { wordpress } = require('wordpress-sdk')

wordpress.initialize({
  // your wordpress rest api base URL
  url: 'https://developer.wordpress.org/wp-json/wp/v2',
})

Fetching posts

wordpress
  .allPosts()
  .then(response => {
    console.log(response.posts)
  })
  .catch(error => {
    console.log(error.message)
  })

Or using async await

try {
  const { posts } = await wordpress.allPosts()
  console.log(posts)
} catch (error) {
  console.log(error.message)
}

Fetching categories

wordpress
  .allCategories()
  .then(response => {
    console.log(response.categories)
  })
  .catch(error => {
    console.log(error.message)
  })

Or using async await

try {
  const { categories } = await wordpress.allCategories()
  console.log(categories)
} catch (error) {
  console.log(error.message)
}