new WPRequest(options)
WPRequest is the base API request object constructor
- Source:
Parameters:
Name | Type | Description | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
Object
|
A hash of options for the WPRequest instance
|
Methods
auth(credentials) → {WPRequest}
Set a request to use authentication, and optionally provide auth credentials
If auth credentials were already specified when the WPAPI instance was created, calling
.auth
on the request chain will set that request to use the existing credentials:
- Source:
Parameters:
Name | Type | Description | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
credentials |
Object
|
An object with 'username' and 'password' string properties, or else a 'nonce' property
|
Examples
request.auth().get...
Alternatively, a username & password (or nonce) can be explicitly passed into `.auth`:
request.auth({
username: 'admin',
password: 'super secure'
}).get...
request.auth({
nonce: 'somenonce'
})...
context(context) → {WPRequest}
Set the context of the request. Used primarily to expose private values on a request object by setting the context to "edit".
- Source:
Parameters:
Name | Type | Description |
---|---|---|
context |
String
|
The context to set on the request |
create(data, callbackopt) → {Promise}
Create the specified resource with the provided data
This is the public interface for creating POST requests
- Source:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
data |
Object
|
The data for the POST request |
|
callback |
function
|
<optional> |
A callback to invoke with the results of the POST request |
Returns:
- Type:
-
Promise
A promise to the results of the HTTP request
delete(dataopt, callbackopt) → {Promise}
Delete the specified resource
- Source:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
data |
Object
|
<optional> |
Data to send along with the DELETE request |
callback |
function
|
<optional> |
A callback to invoke with the results of the DELETE request |
Returns:
- Type:
-
Promise
A promise to the results of the HTTP request
exclude(ids)
Exclude specific resource IDs in the response collection.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
ids |
Number
|
Array.<Number>
|
An ID or array of IDs to exclude |
Returns:
The request instance (for chaining)
file(file, nameopt) → {WPRequest}
Specify a file or a file buffer to attach to the request, for use when creating a new Media item
- Source:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
file |
string
|
object
|
A path to a file (in Node) or an file object (Node or Browser) to attach to the request |
|
name |
string
|
<optional> |
An (optional) filename to use for the file |
Examples
wp.media()
// Pass .file() the file system path to a file to upload
.file( '/path/to/file.jpg' )
.create({})...
wp.media()
// Pass .file() the file reference from an HTML file input
.file( document.querySelector( 'input[type="file"]' ).files[0] )
.create({})...
get(callbackopt) → {Promise}
Get (download the data for) the specified resource
- Source:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
callback |
function
|
<optional> |
A callback to invoke with the results of the GET request |
Returns:
- Type:
-
Promise
A promise to the results of the HTTP request
headers(callbackopt) → {Promise}
Get the headers for the specified resource
- Source:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
callback |
function
|
<optional> |
A callback to invoke with the results of the HEAD request |
Returns:
- Type:
-
Promise
A promise to the header results of the HTTP request
include(ids)
Include specific resource IDs in the response collection.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
ids |
Number
|
Array.<Number>
|
An ID or array of IDs to include |
Returns:
The request instance (for chaining)
namespace(namespace) → {WPRequest}
Set the namespace of the request, e.g. to specify the API root for routes registered by wp core v2 ("wp/v2") or by any given plugin. Any previously- set namespace will be overwritten by subsequent calls to the method.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
namespace |
String
|
A namespace string, e.g. "wp/v2" |
offset(offsetNumber)
Set an arbitrary offset to retrieve items from a specific point in a collection.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
offsetNumber |
Number
|
The number of items by which to offset the response |
Returns:
The request instance (for chaining)
order(direction)
Change the sort direction of a returned collection
- Source:
Parameters:
Name | Type | Description |
---|---|---|
direction |
String
|
The order to use when sorting the response |
Returns:
The request instance (for chaining)
Example
site.comments().order( 'asc' )...
orderby(field)
Order a collection by a specific field
- Source:
Parameters:
Name | Type | Description |
---|---|---|
field |
String
|
The field by which to order the response |
Returns:
The request instance (for chaining)
page(pageNumber)
Set the pagination of a request. Use in conjunction with .perPage()
for explicit
pagination handling. (The number of pages in a response can be retrieved from the
response's _paging.totalPages
property.)
- Source:
Parameters:
Name | Type | Description |
---|---|---|
pageNumber |
Number
|
The page number of results to retrieve |
Returns:
The request instance (for chaining)
param(props, valueopt) → {WPRequest}
Set a parameter to render into the final query URI.
- Source:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
props |
String
|
Object
|
The name of the parameter to set, or an object containing parameter keys and their corresponding values |
|
value |
String
|
Number
|
Array
|
<optional> |
The value of the parameter being set |
perPage(itemsPerPage)
Set the number of items to be returned in a page of responses.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
itemsPerPage |
Number
|
The number of items to return in one page of results |
Returns:
The request instance (for chaining)
search(searchString)
Filter results to those matching the specified search terms.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
searchString |
String
|
A string to search for within post content |
Returns:
The request instance (for chaining)
setHeaders(headers, valueopt) → {WPRequest}
Specify one or more headers to send with the dispatched HTTP request.
- Since:
- 1.1.0
- Source:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
headers |
String
|
Object
|
The name of the header to set, or an object of header names and their associated string values |
|
value |
String
|
<optional> |
The value of the header being set |
Examples
request.setHeaders( 'Authorization', 'Bearer trustme' )...
request.setHeaders({
Authorization: 'Bearer comeonwereoldfriendsright',
'Accept-Language': 'en-CA'
})...
setPathPart(level, val) → {WPRequest}
Set a component of the resource URL itself (as opposed to a query parameter)
If a path component has already been set at this level, throw an error: requests are meant to be transient, so any re-writing of a previously-set path part value is likely to be a mistake.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
level |
Number
|
String
|
A "level" of the path to set, e.g. "1" or "2" |
val |
Number
|
String
|
The value to set at that path part level |
slug(slug)
Query a collection for members with a specific slug.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
slug |
String
|
A post slug (slug), e.g. "hello-world" |
Returns:
The request instance (for chaining)
then(successCallbackopt, failureCallbackopt) → {Promise}
Calling .then on a query chain will invoke the query as a GET and return a promise
- Source:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
successCallback |
function
|
<optional> |
A callback to handle the data returned from the GET request |
failureCallback |
function
|
<optional> |
A callback to handle any errors encountered by the request |
Returns:
- Type:
-
Promise
A promise to the results of the HTTP request
toString() → {String}
Parse the request into a WordPress API request URI string
- Source:
Returns:
- Type:
-
String
The URI for the HTTP request to be sent
validatePath() → {WPRequest}
Validate whether the specified path parts are valid for this endpoint
"Path parts" are non-query-string URL segments, like "some" "path" in the URL
mydomain.com/some/path?and=a&query=string&too
. Because a well-formed path
is necessary to execute a successful API request, we throw an error if the
user has omitted a value (such as /some/[missing component]/url
) or has
provided a path part value that does not match the regular expression the
API uses to goven that segment.
- Source: