eos_wordpress_api
v1.0.40
Published
A simple Wordpress REST API wrapper in javascript
Downloads
8
Maintainers
Readme
wordpress_api
A simple Wordpress REST API wraparound library for Javascript
Index
- Usage
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.