SlideShare a Scribd company logo
WORDPRESS REST API
WORDPRESS REST API
W E B D E V E L O P E R
QEVALON.COM
QEVALON.COM
Valon Cengaj
Valon Cengaj
Outline
Outline
What is the WordPress REST API ?
What is the WordPress REST API ?
Background
Background
Advantages
Advantages
WordPress REST API Commands
WordPress REST API Commands
Authentication
Authentication
Routes & Endpoints
Routes & Endpoints
Custom Routes & Endpoints
Custom Routes & Endpoints
How we can use it
How we can use it
What
What
is the
is the
WordPress
WordPress
REST API ?
REST API ?
An interface that developers can use to access
An interface that developers can use to access
WordPress from outside the WordPress
WordPress from outside the WordPress
installation itself.
installation itself.
REST
REST API
API
JSON
JSON
What REST & API mean?
What REST & API mean?
API
API
A
Application
pplication P
Programming
rogramming I
Interface
nterface
REST
REST
Re
Representational
presentational S
State
tate T
Transfer
ransfer
Each URL is called a request while the data sent back to you is called a response.
It is a set of rules that allow programs to talk to each other.
The developer creates the API on the server and allows the
client to talk to it.
Determines how the API looks like. It is a set of rules that
developers follow when they create their API. One of these
rules states that you should be able to get a piece of data
(called a resource) when you link to a specific URL.
Understanding the
Understanding the
WordPRESS REST API
WordPRESS REST API
BACKGROUND
Was released as part of core in version 4.7 in
December 2016, but it was around as a plugin
before then.
It's designed to support a range of applications
build on WordPress and to transform
WordPress from a CMS to an application
platform.
It's used by the Gutenberg editing interface,
which became part of core in 2018.
Understanding the
Understanding the
WordPRESS REST API
WordPRESS REST API
ADVANTAGES
The ability to create SPAs using the REST API.
The Ability to integrate WordPress with other
frontend technologies and systems.
Specific changes such as the need to build
Gutenberg blocks instead of meta boxes in
page and post editing screens.
The ability to develop with WordPress if you're
a frontend developer who doesn't write PHP.
For PHP developers, an increasing need to
expand your skills to include JavaScript.
WordPress REST API Commands
WordPress REST API Commands
GET Retrieves a resource such as a post or some other data.
Adds a resource to the server, such as a post, attachment
or another resource.
Can be used to edit or update a resource that’s already on
the server.
Removes a resource from the server. Use it with care!
POST
PUT
DELETE
Action
Action Resource
Resource
/posts
/posts
Description
Description
/posts/5
/posts/5
Authentication
Authentication
Only applicable if the REST API is used within WordPress
Cookie authentication
Cookie authentication
Using JWT Authentication plugin by Enrique Chavez
Token Autherization
Token Autherization
Can be used ONLY during development
Uses Username & Password as security details
Has plugins for assisting the process (github.com/WP-API/Basic-Auth)
Basic Authentication
Basic Authentication
ROUTES
ROUTES
out of the box
out of the box
Resource Base Route
Posts /wp/v2/posts
Post Revisions /wp/v2/posts/<id>/revisions
Categories /wp/v2/categories
Tags /wp/v2/tags
Pages /wp/v2/pages
Page Revisions /wp/v2/pages<id>/revisions
Comments /wp/v2/posts/comments
Taxonomies /wp/v2/taxonomies
Media /wp/v2/media
Users /wp/v2/users
Post Types /wp/v2/types
Post Statuses /wp/v2/statuses
Settings /wp/v2/settings
Themes /wp/v2/themes
DEMO
DEMO
Jump to Code
Jump to Code
CUSTOM ROUTES AND ENDPOINTS
CUSTOM ROUTES AND ENDPOINTS
Creating a custom endpoint
Creating a custom endpoint
<?php
add_action('rest_api_init', function () {
register_rest_route( '/wprestapi/v2', '/books', [
'methods' => 'GET',
'callback' => 'rest_get_books'
]);
});
?>
Fetching data using the created endpoint
Fetching data using the created endpoint
<?php
function rest_get_books() {
$args = array(
'post_type' => 'books',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
);
$posts = new WP_Query( $args );
if ( $posts->have_posts() ):
while ( $posts->have_posts() ) : $posts->the_post();
$postID = get_the_id();
$postLink = get_the_permalink();
$postTitle = get_the_title();
$postContent = get_the_content();
$postData[] = [
'id' => $postID,
'link' => $postLink,
'title' => $postTitle,
'content' => $postContent
];
endwhile;
endif;
wp_reset_postdata();
return $postData;
}
Display data using jQuery
Display data using jQuery
var ajaxurl = "http://localhost:8888/wordpressrestapi/wp-json/wprestapi/v2/books";
jQuery(document).ready(function ($) {
var data = {
'action': 'rest_get_books',
}
$.get(ajaxurl, data, function (response) {
$.each(response, function(i) {
var posts = response[i];
jQuery('.books-wrapper').append('<h1>' + posts.title + '</h1> <br> <p>' + posts.content + '<p>');
});
})
})
Real-World Applications using the WordPress REST API
Real-World Applications using the WordPress REST API
WordPress.com
WordPress.com
The Block Editor (Gutenberg)
The Block Editor (Gutenberg)
USA Today - Newspaper
USA Today - Newspaper
USTWO - Digital Product Studio
USTWO - Digital Product Studio
Thank You!
Thank You!
QEVALON.COM
QEVALON.COM
Ad

More Related Content

Similar to WORDPRESS_REST_API_WORDPRESS_REST_API.pdf (20)

Single Page Web Applications with WordPress REST API
Single Page Web Applications with WordPress REST APISingle Page Web Applications with WordPress REST API
Single Page Web Applications with WordPress REST API
Tejaswini Deshpande
 
Php Asp Net Interoperability Rc Jao
Php Asp Net Interoperability Rc JaoPhp Asp Net Interoperability Rc Jao
Php Asp Net Interoperability Rc Jao
jedt
 
Restful Integration with WSO2 ESB
Restful Integration with WSO2 ESB Restful Integration with WSO2 ESB
Restful Integration with WSO2 ESB
WSO2
 
The WordPress REST API as a Springboard for Website Greatness
The WordPress REST API as a Springboard for Website GreatnessThe WordPress REST API as a Springboard for Website Greatness
The WordPress REST API as a Springboard for Website Greatness
WP Engine UK
 
REST Development made Easy with ColdFusion Aether
REST Development made Easy with ColdFusion AetherREST Development made Easy with ColdFusion Aether
REST Development made Easy with ColdFusion Aether
Pavan Kumar
 
PHP on Windows and on Azure
PHP on Windows and on AzurePHP on Windows and on Azure
PHP on Windows and on Azure
Maarten Balliauw
 
Play with force.com metadata
Play with force.com metadataPlay with force.com metadata
Play with force.com metadata
Rakesh Kumar Kedia
 
Old WP REST API, New Tricks
Old WP REST API, New TricksOld WP REST API, New Tricks
Old WP REST API, New Tricks
WordPress Community Montreal
 
Getting Started with API Management
Getting Started with API ManagementGetting Started with API Management
Getting Started with API Management
Revelation Technologies
 
Api Design and More (Friday Training at Itnig)
Api Design and More (Friday Training at Itnig)Api Design and More (Friday Training at Itnig)
Api Design and More (Friday Training at Itnig)
itnig
 
Using WordPress as a web application platform
Using WordPress as a web application platformUsing WordPress as a web application platform
Using WordPress as a web application platform
Joe Querin
 
WordPress and Client Side Web Applications WCTO
WordPress and Client Side Web Applications WCTOWordPress and Client Side Web Applications WCTO
WordPress and Client Side Web Applications WCTO
Roy Sivan
 
So, You Wanna Dev? Join the Team! - WordCamp Raleigh 2017
So, You Wanna Dev? Join the Team! - WordCamp Raleigh 2017 So, You Wanna Dev? Join the Team! - WordCamp Raleigh 2017
So, You Wanna Dev? Join the Team! - WordCamp Raleigh 2017
Evan Mullins
 
How to Create a Headless WordPress Site with ReactJs.pdf
How to Create a Headless WordPress Site with ReactJs.pdfHow to Create a Headless WordPress Site with ReactJs.pdf
How to Create a Headless WordPress Site with ReactJs.pdf
WPWeb Infotech
 
Nodejs Intro - Part2 Introduction to Web Applications
Nodejs Intro - Part2 Introduction to Web ApplicationsNodejs Intro - Part2 Introduction to Web Applications
Nodejs Intro - Part2 Introduction to Web Applications
Budh Ram Gurung
 
Amazon API Gateway
Amazon API GatewayAmazon API Gateway
Amazon API Gateway
Mark Bate
 
Crafting APIs
Crafting APIsCrafting APIs
Crafting APIs
Tatiana Al-Chueyr
 
WordPress Plugin Development- Rich Media Institute Workshop
WordPress Plugin Development- Rich Media Institute WorkshopWordPress Plugin Development- Rich Media Institute Workshop
WordPress Plugin Development- Rich Media Institute Workshop
Brendan Sera-Shriar
 
Introduction to WordPress Rest API
Introduction to WordPress Rest APIIntroduction to WordPress Rest API
Introduction to WordPress Rest API
kishore sahoo
 
Developing Plugins For WordPress
Developing Plugins For WordPressDeveloping Plugins For WordPress
Developing Plugins For WordPress
Lester Chan
 
Single Page Web Applications with WordPress REST API
Single Page Web Applications with WordPress REST APISingle Page Web Applications with WordPress REST API
Single Page Web Applications with WordPress REST API
Tejaswini Deshpande
 
Php Asp Net Interoperability Rc Jao
Php Asp Net Interoperability Rc JaoPhp Asp Net Interoperability Rc Jao
Php Asp Net Interoperability Rc Jao
jedt
 
Restful Integration with WSO2 ESB
Restful Integration with WSO2 ESB Restful Integration with WSO2 ESB
Restful Integration with WSO2 ESB
WSO2
 
The WordPress REST API as a Springboard for Website Greatness
The WordPress REST API as a Springboard for Website GreatnessThe WordPress REST API as a Springboard for Website Greatness
The WordPress REST API as a Springboard for Website Greatness
WP Engine UK
 
REST Development made Easy with ColdFusion Aether
REST Development made Easy with ColdFusion AetherREST Development made Easy with ColdFusion Aether
REST Development made Easy with ColdFusion Aether
Pavan Kumar
 
PHP on Windows and on Azure
PHP on Windows and on AzurePHP on Windows and on Azure
PHP on Windows and on Azure
Maarten Balliauw
 
Api Design and More (Friday Training at Itnig)
Api Design and More (Friday Training at Itnig)Api Design and More (Friday Training at Itnig)
Api Design and More (Friday Training at Itnig)
itnig
 
Using WordPress as a web application platform
Using WordPress as a web application platformUsing WordPress as a web application platform
Using WordPress as a web application platform
Joe Querin
 
WordPress and Client Side Web Applications WCTO
WordPress and Client Side Web Applications WCTOWordPress and Client Side Web Applications WCTO
WordPress and Client Side Web Applications WCTO
Roy Sivan
 
So, You Wanna Dev? Join the Team! - WordCamp Raleigh 2017
So, You Wanna Dev? Join the Team! - WordCamp Raleigh 2017 So, You Wanna Dev? Join the Team! - WordCamp Raleigh 2017
So, You Wanna Dev? Join the Team! - WordCamp Raleigh 2017
Evan Mullins
 
How to Create a Headless WordPress Site with ReactJs.pdf
How to Create a Headless WordPress Site with ReactJs.pdfHow to Create a Headless WordPress Site with ReactJs.pdf
How to Create a Headless WordPress Site with ReactJs.pdf
WPWeb Infotech
 
Nodejs Intro - Part2 Introduction to Web Applications
Nodejs Intro - Part2 Introduction to Web ApplicationsNodejs Intro - Part2 Introduction to Web Applications
Nodejs Intro - Part2 Introduction to Web Applications
Budh Ram Gurung
 
Amazon API Gateway
Amazon API GatewayAmazon API Gateway
Amazon API Gateway
Mark Bate
 
WordPress Plugin Development- Rich Media Institute Workshop
WordPress Plugin Development- Rich Media Institute WorkshopWordPress Plugin Development- Rich Media Institute Workshop
WordPress Plugin Development- Rich Media Institute Workshop
Brendan Sera-Shriar
 
Introduction to WordPress Rest API
Introduction to WordPress Rest APIIntroduction to WordPress Rest API
Introduction to WordPress Rest API
kishore sahoo
 
Developing Plugins For WordPress
Developing Plugins For WordPressDeveloping Plugins For WordPress
Developing Plugins For WordPress
Lester Chan
 

Recently uploaded (20)

acid base ppt and their specific application in food
acid base ppt and their specific application in foodacid base ppt and their specific application in food
acid base ppt and their specific application in food
Fatehatun Noor
 
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning ModelsMode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Journal of Soft Computing in Civil Engineering
 
Evonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdfEvonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdf
szhang13
 
Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 
Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
Personal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.pptPersonal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.ppt
ganjangbegu579
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
Working with USDOT UTCs: From Conception to Implementation
Working with USDOT UTCs: From Conception to ImplementationWorking with USDOT UTCs: From Conception to Implementation
Working with USDOT UTCs: From Conception to Implementation
Alabama Transportation Assistance Program
 
Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...
Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...
Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...
Journal of Soft Computing in Civil Engineering
 
Applications of Centroid in Structural Engineering
Applications of Centroid in Structural EngineeringApplications of Centroid in Structural Engineering
Applications of Centroid in Structural Engineering
suvrojyotihalder2006
 
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
ijflsjournal087
 
Nanometer Metal-Organic-Framework Literature Comparison
Nanometer Metal-Organic-Framework  Literature ComparisonNanometer Metal-Organic-Framework  Literature Comparison
Nanometer Metal-Organic-Framework Literature Comparison
Chris Harding
 
Automatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and BeyondAutomatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and Beyond
NU_I_TODALAB
 
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdf
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdfSmart City is the Future EN - 2024 Thailand Modify V1.0.pdf
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdf
PawachMetharattanara
 
2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt
rakshaiya16
 
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
PawachMetharattanara
 
Frontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend EngineersFrontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend Engineers
Michael Hertzberg
 
Modeling the Influence of Environmental Factors on Concrete Evaporation Rate
Modeling the Influence of Environmental Factors on Concrete Evaporation RateModeling the Influence of Environmental Factors on Concrete Evaporation Rate
Modeling the Influence of Environmental Factors on Concrete Evaporation Rate
Journal of Soft Computing in Civil Engineering
 
JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...
JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...
JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...
Reflections on Morality, Philosophy, and History
 
Generative AI & Large Language Models Agents
Generative AI & Large Language Models AgentsGenerative AI & Large Language Models Agents
Generative AI & Large Language Models Agents
aasgharbee22seecs
 
acid base ppt and their specific application in food
acid base ppt and their specific application in foodacid base ppt and their specific application in food
acid base ppt and their specific application in food
Fatehatun Noor
 
Evonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdfEvonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdf
szhang13
 
Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 
Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
Personal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.pptPersonal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.ppt
ganjangbegu579
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
Applications of Centroid in Structural Engineering
Applications of Centroid in Structural EngineeringApplications of Centroid in Structural Engineering
Applications of Centroid in Structural Engineering
suvrojyotihalder2006
 
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
ijflsjournal087
 
Nanometer Metal-Organic-Framework Literature Comparison
Nanometer Metal-Organic-Framework  Literature ComparisonNanometer Metal-Organic-Framework  Literature Comparison
Nanometer Metal-Organic-Framework Literature Comparison
Chris Harding
 
Automatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and BeyondAutomatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and Beyond
NU_I_TODALAB
 
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdf
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdfSmart City is the Future EN - 2024 Thailand Modify V1.0.pdf
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdf
PawachMetharattanara
 
2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt
rakshaiya16
 
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
PawachMetharattanara
 
Frontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend EngineersFrontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend Engineers
Michael Hertzberg
 
Generative AI & Large Language Models Agents
Generative AI & Large Language Models AgentsGenerative AI & Large Language Models Agents
Generative AI & Large Language Models Agents
aasgharbee22seecs
 
Ad

WORDPRESS_REST_API_WORDPRESS_REST_API.pdf

  • 2. W E B D E V E L O P E R QEVALON.COM QEVALON.COM Valon Cengaj Valon Cengaj
  • 3. Outline Outline What is the WordPress REST API ? What is the WordPress REST API ? Background Background Advantages Advantages WordPress REST API Commands WordPress REST API Commands Authentication Authentication Routes & Endpoints Routes & Endpoints Custom Routes & Endpoints Custom Routes & Endpoints How we can use it How we can use it
  • 4. What What is the is the WordPress WordPress REST API ? REST API ? An interface that developers can use to access An interface that developers can use to access WordPress from outside the WordPress WordPress from outside the WordPress installation itself. installation itself. REST REST API API JSON JSON
  • 5. What REST & API mean? What REST & API mean? API API A Application pplication P Programming rogramming I Interface nterface REST REST Re Representational presentational S State tate T Transfer ransfer Each URL is called a request while the data sent back to you is called a response. It is a set of rules that allow programs to talk to each other. The developer creates the API on the server and allows the client to talk to it. Determines how the API looks like. It is a set of rules that developers follow when they create their API. One of these rules states that you should be able to get a piece of data (called a resource) when you link to a specific URL.
  • 6. Understanding the Understanding the WordPRESS REST API WordPRESS REST API BACKGROUND Was released as part of core in version 4.7 in December 2016, but it was around as a plugin before then. It's designed to support a range of applications build on WordPress and to transform WordPress from a CMS to an application platform. It's used by the Gutenberg editing interface, which became part of core in 2018.
  • 7. Understanding the Understanding the WordPRESS REST API WordPRESS REST API ADVANTAGES The ability to create SPAs using the REST API. The Ability to integrate WordPress with other frontend technologies and systems. Specific changes such as the need to build Gutenberg blocks instead of meta boxes in page and post editing screens. The ability to develop with WordPress if you're a frontend developer who doesn't write PHP. For PHP developers, an increasing need to expand your skills to include JavaScript.
  • 8. WordPress REST API Commands WordPress REST API Commands GET Retrieves a resource such as a post or some other data. Adds a resource to the server, such as a post, attachment or another resource. Can be used to edit or update a resource that’s already on the server. Removes a resource from the server. Use it with care! POST PUT DELETE Action Action Resource Resource /posts /posts Description Description /posts/5 /posts/5
  • 9. Authentication Authentication Only applicable if the REST API is used within WordPress Cookie authentication Cookie authentication Using JWT Authentication plugin by Enrique Chavez Token Autherization Token Autherization Can be used ONLY during development Uses Username & Password as security details Has plugins for assisting the process (github.com/WP-API/Basic-Auth) Basic Authentication Basic Authentication
  • 10. ROUTES ROUTES out of the box out of the box Resource Base Route Posts /wp/v2/posts Post Revisions /wp/v2/posts/<id>/revisions Categories /wp/v2/categories Tags /wp/v2/tags Pages /wp/v2/pages Page Revisions /wp/v2/pages<id>/revisions Comments /wp/v2/posts/comments Taxonomies /wp/v2/taxonomies Media /wp/v2/media Users /wp/v2/users Post Types /wp/v2/types Post Statuses /wp/v2/statuses Settings /wp/v2/settings Themes /wp/v2/themes
  • 12. CUSTOM ROUTES AND ENDPOINTS CUSTOM ROUTES AND ENDPOINTS
  • 13. Creating a custom endpoint Creating a custom endpoint <?php add_action('rest_api_init', function () { register_rest_route( '/wprestapi/v2', '/books', [ 'methods' => 'GET', 'callback' => 'rest_get_books' ]); }); ?>
  • 14. Fetching data using the created endpoint Fetching data using the created endpoint <?php function rest_get_books() { $args = array( 'post_type' => 'books', 'post_status' => 'publish', 'posts_per_page' => -1, 'orderby' => 'date', ); $posts = new WP_Query( $args ); if ( $posts->have_posts() ): while ( $posts->have_posts() ) : $posts->the_post(); $postID = get_the_id(); $postLink = get_the_permalink(); $postTitle = get_the_title(); $postContent = get_the_content(); $postData[] = [ 'id' => $postID, 'link' => $postLink, 'title' => $postTitle, 'content' => $postContent ]; endwhile; endif; wp_reset_postdata(); return $postData; }
  • 15. Display data using jQuery Display data using jQuery var ajaxurl = "http://localhost:8888/wordpressrestapi/wp-json/wprestapi/v2/books"; jQuery(document).ready(function ($) { var data = { 'action': 'rest_get_books', } $.get(ajaxurl, data, function (response) { $.each(response, function(i) { var posts = response[i]; jQuery('.books-wrapper').append('<h1>' + posts.title + '</h1> <br> <p>' + posts.content + '<p>'); }); }) })
  • 16. Real-World Applications using the WordPress REST API Real-World Applications using the WordPress REST API WordPress.com WordPress.com The Block Editor (Gutenberg) The Block Editor (Gutenberg) USA Today - Newspaper USA Today - Newspaper USTWO - Digital Product Studio USTWO - Digital Product Studio
  翻译: