Introduction
WordPress REST API jQuery is still fresh so there will be changes to the documentation over time as the script is developed.
Requirements
You must be using:
- WordPress 4.4+.
- You may make requests over either HTTP or HTTPS, but HTTPS is recommended where possible.
Parameters
Parameter | Default | Description |
---|---|---|
site_url | Default is the current host name. Only set if connecting with another site. | |
namespace | wp/v2 | Default is current WordPress REST API. Set this to use other REST API’s registered like JetPack, WooCommerce etc. |
endpoint | posts | See the endpoints for reference. |
post_data | {} | Use this to pass data for posting or updating a request. |
form_method | GET | GET, POST, UPDATE, DELETE |
data_type | json | Use jsonp for cross-domain support |
Setting the Site URL
var posts = restjQuery(
site_url: "http://example.com"
);
This lets you get posts or any other endpoint you set from a specific WordPress site.
Setting the Namespace
var products = restjQuery(
namespace: "wc/v1",
endpoint: "products"
);
This lets you get data from other registered REST API’s like WooCommerce as shown in the example. This will also need to be accompanied by the endpoint
parameter.
Authentication
Authentication is still in development.
Posts
Get Posts
var posts = restjQuery();
JSON response example:
[
{
"id":1,
"date":"2017-01-04T15:46:25",
"date_gmt":"2017-01-04T15:46:25",
"guid":{
"rendered":"http:\/\/experiments.dev\/?p=1"
},
"modified":"2017-01-04T15:46:25",
"modified_gmt":"2017-01-04T15:46:25",
"slug":"hello-world",
"status":"publish",
"type":"post",
"link":"https:\/\/experiments.dev\/hello-world\/",
"title":{
"rendered":"Hello world!"
},
"content":{
"rendered":"<p>Welcome to WordPress. This is your first post. Edit or delete it, then start writing!<\/p>\n",
"protected":false
},
"excerpt":{
"rendered":"<p>Welcome to WordPress. This is your first post. Edit or delete it, then start writing!<\/p>\n",
"protected":false
},
"author":1,
"featured_media":0,
"comment_status":"open",
"ping_status":"open",
"sticky":false,
"template":"",
"format":"standard",
"meta":[],
"categories":[1],
"tags":[],
"_links":{
"self":[
{
"href":"https:\/\/experiments.dev\/wp-json\/wp\/v2\/posts\/1"
}
],
"collection":[
{
"href":"https:\/\/experiments.dev\/wp-json\/wp\/v2\/posts"
}
],
"about":[
{
"href":"https:\/\/experiments.dev\/wp-json\/wp\/v2\/types\/post"
}
],
"author":[
{
"embeddable":true,
"href":"https:\/\/experiments.dev\/wp-json\/wp\/v2\/users\/1"
}
],
"replies":[
{
"embeddable":true,
"href":"https:\/\/experiments.dev\/wp-json\/wp\/v2\/comments?post=1"
}
],
"version-history":[
{
"href":"https:\/\/experiments.dev\/wp-json\/wp\/v2\/posts\/1\/revisions"
}
],
"wp:attachment":[
{
"href":"https:\/\/experiments.dev\/wp-json\/wp\/v2\/media?parent=1"
}
],
"wp:term":[
{
"taxonomy":"category",
"embeddable":true,
"href":"https:\/\/experiments.dev\/wp-json\/wp\/v2\/categories?post=1"
},
{
"taxonomy":"post_tag",
"embeddable":true,
"href":"https:\/\/experiments.dev\/wp-json\/wp\/v2\/tags?post=1"
}
],
"curies":[
{
"name":"wp",
"href":"https:\/\/api.w.org\/{rel}",
"templated":true
}
]
}
}
]
This lets you retrieve posts.
HTTP Request
/wp-json/wp/v2/posts
Get a Single Post
var hello_world = restjQuery(
endpoint: "posts/1"
);
JSON response example:
{
"id":1,
"date":"2017-01-04T15:46:25",
"date_gmt":"2017-01-04T15:46:25",
"guid":{
"rendered":"http:\/\/experiments.dev\/?p=1"
},
"modified":"2017-01-04T15:46:25",
"modified_gmt":"2017-01-04T15:46:25",
"slug":"hello-world",
"status":"publish",
"type":"post",
"link":"https:\/\/experiments.dev\/hello-world\/",
"title":{
"rendered":"Hello world!"
},
"content":{
"rendered":"<p>Welcome to WordPress. This is your first post. Edit or delete it, then start writing!<\/p>\n",
"protected":false
},
"excerpt":{
"rendered":"<p>Welcome to WordPress. This is your first post. Edit or delete it, then start writing!<\/p>\n",
"protected":false
},
"author":1,
"featured_media":0,
"comment_status":"open",
"ping_status":"open",
"sticky":false,
"template":"",
"format":"standard",
"meta":[],
"categories":[1],
"tags":[],
"_links":{
"self":[
{
"href":"https:\/\/experiments.dev\/wp-json\/wp\/v2\/posts\/1"
}
],
"collection":[
{
"href":"https:\/\/experiments.dev\/wp-json\/wp\/v2\/posts"
}
],
"about":[
{
"href":"https:\/\/experiments.dev\/wp-json\/wp\/v2\/types\/post"
}
],
"author":[
{
"embeddable":true,
"href":"https:\/\/experiments.dev\/wp-json\/wp\/v2\/users\/1"
}
],
"replies":[
{
"embeddable":true,
"href":"https:\/\/experiments.dev\/wp-json\/wp\/v2\/comments?post=1"
}
],
"version-history":[
{
"href":"https:\/\/experiments.dev\/wp-json\/wp\/v2\/posts\/1\/revisions"
}
],
"wp:attachment":[
{
"href":"https:\/\/experiments.dev\/wp-json\/wp\/v2\/media?parent=1"
}
],
"wp:term":[
{
"taxonomy":"category",
"embeddable":true,
"href":"https:\/\/experiments.dev\/wp-json\/wp\/v2\/categories?post=1"
},
{
"taxonomy":"post_tag",
"embeddable":true,
"href":"https:\/\/experiments.dev\/wp-json\/wp\/v2\/tags?post=1"
}
],
"curies":[
{
"name":"wp",
"href":"https:\/\/api.w.org\/{rel}",
"templated":true
}
]
}
}
This lets you retrieve and view a specific post.
HTTP Request
/wp-json/wp/v2/posts/1
Pages
Get Pages
var pages = restjQuery(
endpoint: "pages"
);
JSON response example:
code response goes here
This lets you retrieve pages.
HTTP Request
/wp-json/wp/v2/pages
Get a Single Page
var sample_page = restjQuery(
endpoint: "pages/2"
);
JSON response example:
code response goes here
This lets you retrieve and view a specific page.
HTTP Request
/wp-json/wp/v2/pages/2
Comments
Get Comments
var comments = restjQuery(
endpoint: "comments"
);
JSON response example:
code response goes here
This lets you retrieve comments.
HTTP Request
/wp-json/wp/v2/comments
Get a Single Comment
var comments = restjQuery(
endpoint: "comments/1"
);
JSON response example:
code response goes here
This lets you retrieve and view a specific comment.
HTTP Request
/wp-json/wp/v2/comments/1
Categories
Get Categories
var categories = restjQuery(
endpoint: "categories"
);
JSON response example:
code response goes here
This lets you retrieve categories.
HTTP Request
/wp-json/wp/v2/categories
Get a Single Category
var categories = restjQuery(
endpoint: "categories/1"
);
JSON response example:
code response goes here
This lets you retrieve and view a specific category.
HTTP Request
/wp-json/wp/v2/categories/1
Tags
Get Tags
var tags = restjQuery(
endpoint: "tags"
);
JSON response example:
code response goes here
This lets you retrieve tags.
HTTP Request
/wp-json/wp/v2/tags
Get a Single Tag
var tags = restjQuery(
endpoint: "tags/1"
);
JSON response example:
code response goes here
This lets you retrieve and view a specific tag.
HTTP Request
/wp-json/wp/v2/tags/1
Taxonomies
Get Taxonomies
var taxonomies = restjQuery(
endpoint: "taxonomies"
);
JSON response example:
code response goes here
This lets you retrieve taxonomies.
HTTP Request
/wp-json/wp/v2/taxonomies
Get a Single Taxonomy
var taxonomies = restjQuery(
endpoint: "taxonomies/1"
);
JSON response example:
code response goes here
This lets you retrieve and view a specific taxonomy.
HTTP Request
/wp-json/wp/v2/taxonomies/1
Users
Get Users
var users = restjQuery(
endpoint: "users"
);
JSON response example:
code response goes here
This lets you retrieve users.
HTTP Request
/wp-json/wp/v2/users
Get a Single User
var users = restjQuery(
endpoint: "users/1"
);
JSON response example:
code response goes here
This lets you retrieve and view a specific user.
HTTP Request
/wp-json/wp/v2/users/1
Post Types
Get Post Types
var post_types = restjQuery(
endpoint: "types"
);
JSON response example:
code response goes here
This lets you retrieve post types.
HTTP Request
/wp-json/wp/v2/types
Get a Single Post Type
var post_types = restjQuery(
endpoint: "types/1"
);
JSON response example:
code response goes here
This lets you retrieve and view a specific post type.
HTTP Request
/wp-json/wp/v2/types/1
Post Statuses
Get Post Statuses
var post_statuses = restjQuery(
endpoint: "statuses"
);
JSON response example:
code response goes here
This lets you retrieve post statuses.
HTTP Request
/wp-json/wp/v2/statuses
Get a Single Post Status
var post_statuses = restjQuery(
endpoint: "statuses/1"
);
JSON response example:
code response goes here
This lets you retrieve and view a specific post status.
HTTP Request
/wp-json/wp/v2/statuses/1
Settings
Get Settings
var settings = restjQuery(
endpoint: "settings"
);
JSON response example:
code response goes here
This lets you retrieve settings.
HTTP Request
/wp-json/wp/v2/settings
Get a Single Setting
var settings = restjQuery(
endpoint: "settings/1"
);
JSON response example:
code response goes here
This lets you retrieve and view a specific setting.