1.0.7 • Published 5 years ago

use-wordpress v1.0.7

Weekly downloads
3
License
ISC
Repository
github
Last release
5 years ago

Build Status

Overview

Useful React Hooks for WordPress.

Requirements

This library relies upon an implementation of WHATWG fetch and URLSearchParams.

Initialization

useWordPress(nonce: string, baseUri: string)

Usage

import React from 'react'
import { useWordPress } from 'use-wordpress'
export const TestComponent = props => {
  const { data, get, total, totalPages } = useWordPress(wpApiSettings.nonce)
  const [initialized, setInitialized] = React.useState(false)
  React.useEffect(() => {
    if (!initialized) {
      get('categories') // Fetches categories and assigns them to data
      setInitialized(true) 
    }
  }, [])
  return (
    // ...
  )
}

Arguments

nametyperequireddefaultdescription
noncestringyesA "number used once" to help protect URLs and forms from certain types of misuse.
baseUristringno?/rest_route=/wp/v2The base URI of WordPress's REST API.

API

Properties

nametypedescription
dataanyThe response from the fetch call.
embeddedObjectStores the _embedded property of an entity if it exists.
featuredMediaArray<Object>Stores the wp:featuredmedia array of embedded if it exists.
errorErrorAny errors returned from the fetch calls.
loadingbooleanWhether or not a fetch is currently being performed.
totalNumberThe total number of records in the collection.
totalPagesNumberthe total number of pages encompassing all available records.

Methods

fetchData (endpoint: string, callback: Function, options: Object)

A wrapper for window.fetch(). Populates total and totalPages with the values of x-wp-total and x-wp-totalpages, respectively, if they are present in the response header.

nametyperequireddefaultdescription
endpointstringyesThe portion of the URI that is appended on the base URI passed into the initializer.
callbackFunctionyesA callback that invoked upon a successful fetch request.
optionsObjectnoOptions passed to the fetch call.

get (type: string, options: {[key: string]: string}, getEmbedded: boolean)

Used to fetch a collection of type.

nametyperequireddefaultdescription
typestringnopostsThe type of record your are fetch (i.e posts, pages, categories).
optionsSearchParamsno{}An object that is converted into search parameters.
getEmbeddedbooleannofalseIf true it will add the _embed search parameter to the fetch.

getById (id: number, type: string, options: {[key: string]: string, getEmbedded: boolean)

Used to fetch a record by its ID.

nametyperequireddefaultdescription
idnumberyesThe ID of the record your are trying to fetch.
typestringyespostsThe type of record your are fetch (i.e posts, pages, categories).
optionsSearchParamsno{}An object that is converted into search parameters.
getEmbeddedbooleannofalseIf true it will add the _embed search parameter to the fetch.

getBySlug (slug: string, type: string, options: {[key: string]: string, getEmbedded: boolean)

Used to fetch a record by its slug.

nametyperequireddefaultdescription
slugstringyesThe slug of the record your are trying to fetch.
typestringyespostsThe type of record your are fetch (i.e posts, pages, categories).
optionsSearchParamsno{}An object that is converted into search parameters.
getEmbeddedbooleannofalseIf true it will add the _embed search parameter to the fetch.

getEmbedded (entity: {'_embedded': Object})

Stores the _embedded of entity if it exists. The value of _embedded is assigned to embedded (see "Properties").

nametyperequireddefaultdescription
entity{'_embedded': Object}yesThe data source.

getFeaturedMedia (embedded: {'wp:featuredmedia': Array<Object>})

Stores the wp:featuredmedia of embedded if it exists. The value of wp:featuredmedia is stored in featuredMedia (see "Properties").

nametyperequireddefaultdescription
embedded{'wp:featuredmedia': Array<Object>}yesThe data source.

getPageById (id: number, options: Object, getEmbedded: boolean)

A wrapper for getById used to get a page by its ID.

nametyperequireddefaultdescription
idnumberyesThe ID of the page.
optionsSearchParamsno{}An object that is converted into the request search params.
getEmbeddedbooleannotrueIf true the request will also fetch embedded objects.

getPageBySlug (slug: string, options: Object, getEmbedded: boolean)

A wrapper for getBySlug used to get a page by its slug.

nametyperequireddefaultdescription
slugstringyesThe slug of the page.
optionsSearchParamsno{}An object that is converted into the request search params.
getEmbeddedbooleannotrueIf true the request will also fetch embedded objects.

Data Types

SearchParams: {[key: string]: string}

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago