1.0.40 • Published 3 years ago

eos_wordpress_api v1.0.40

Weekly downloads
46
License
MIT
Repository
github
Last release
3 years ago

wordpress_api

A simple Wordpress REST API wraparound library for Javascript

Index

Usage

Installation:

npm install eos_wordpress_api

Initialising:

Change 'path_to_api' to the full URL of your wordpress rest route e.g. https://www.example.com/wp-json/

//import the WordpressApi library
import WordpressApi from 'eos_wordpress_api'

//for convenience you can make it available globally
window.WordpressApi = WordpressApi;
//where you want to use it
//Do not use this class globally 
const myapi = new WordpressApi('path_to_api');

Endpoints:

Endpoints are wrappers around axios calls and return a Promise with the axios returned response.

Custom Endpoints:

Need to be added to your Wordpress functions.php file see Wordpress API documentation on custom endpoints

frontPage()

Gets the page listed as the home or front page on the Wordpress control panel

myapi.frontPage()
.then(({data}) => {
    const myfrontpage = data;
})

headerImage()

Returns the image designated as the site header image

myapi.headerImage()

menu(slug)

Returns the menu identified by the slug (not ID) e.g. 'top-menu'

myapi.menu('top-menu')

sidebars()

Returns all the sidebars as an array of sidebars, this includes in each sidebar the html of any widget in the sidebar

myapi.sidebars()
.then(({data}) => {
    const sidebars = data;
    sidebars.forEach(sidebar => {
        const widgets = sidebar.widgets;
        //do something with the widgets
    })
})

siteLogo()

Returns the image url designated as the Site Logo, can be used in img src

Built In Endpoints:

These are the builtin endpoints in the Wordpress REST Api and do not require any configuration in Wordpress and can be used on any Wordpress site

mediaItem(id)

Returns the image url of media item identified by id

const featuredImage = mypost.featured_media;
myapi.mediaItem(featuredImage)
.then(({data}) => {
    const imgURL = data;
})

page(id)

Returns a specific page identified by id

pages()

Returns a collection of pages (all public pages unless filtered)

post(id)

Returns a specific post identified by id

posts()

Returns a collection of pages (all public pages unless filtered)

search(searchString)

Searchs the site for the searchString and returns any posts or pages matching the criteria. This is a wrapper around the Wordpress API search endpoint and therefore only returns a list of items with the following fields: ID, Title, URL, Type, Subtype. See Wordpress API documentation for further details. It does not return full posts or pages.

settings()

For Future use, currently this function does not work as it needs Authentication

Utility functions:

sort_menu(menu)

Takes the result of a call to menu() and sorts the results and creates a hierarchy according to parent and child menu items. Works to one sub level only

recentPosts(limit)

Returns a number of the most recent posts. The number returned is limited to limit, otherwise this is similar to posts()

Filter Search Functions:

All the following functions must be used with either posts() or pages(), they do not return results by themselves. Functions can be chained in any order but the post of page functions must be last. e.g.

tags([1,3]).fields(['title', 'content']).posts()

searchInPosts(searchString)

Unlike the search function above this is a filter function which can be chained with other filter functions and used before posts() or pages() to filter the results

myapi.searchInPosts('something to search for').posts()
.then(({data}) => {
    const my_search_for_posts = data;
})

tags('array of tag IDs')

A filter function used with posts() or pages() to limit results to the tags supplied, NB this function and Wordpress requires tag IDs and not the name of the tag e.g. tags('popularTag') will not work instead use tags('1,2,3')

categories('array of category IDs')

Similar to tags this function tages an array of numerical category IDs

fields('array of fields to return')

Limits the returns results to the fields supplied

orderby(field, order='desc')

Orders the returned results by the field supplied, the default order is decending, change to asc for ascending. NB if you use this with the fields function make sure you include the orderby field in the array of fields to return

limit(number)

Limits the number or rows to return to 'number'

sticky()

Filters the returned results to posts designated as sticky, NB this has not effect when returning pages

exclude_categorys()

Excludes from the results any posts or pages with a category of 'hidden'. This is not a Wordpress property or built in category but a category you need to create.

1.0.39

3 years ago

1.0.40

3 years ago

1.0.38

3 years ago

1.0.37

3 years ago

1.0.36

3 years ago

1.0.35

3 years ago

1.0.19

3 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.20

3 years ago

1.0.26

3 years ago

1.0.25

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.29

3 years ago

1.0.28

3 years ago

1.0.27

3 years ago

1.0.33

3 years ago

1.0.32

3 years ago

1.0.31

3 years ago

1.0.30

3 years ago

1.0.34

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago