Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
REST APIs for Absolute Beginners
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Tom J Nowell
July 23, 2017
Technology
1k
0
Share
REST APIs for Absolute Beginners
How to make a basic endpoint, use it on the frontend, and convert existing code
Tom J Nowell
July 23, 2017
More Decks by Tom J Nowell
See All by Tom J Nowell
Using Blocks Outside The Editor
tarendai
0
1.1k
Composer_and_WordPress__1_.pdf
tarendai
0
95
VVV 2
tarendai
0
830
WordCamp Europe 2016 - Handling Anxiety
tarendai
1
540
Escape From New York
tarendai
0
780
WP The Right Way
tarendai
0
1.1k
Code Deodorant 2014
tarendai
1
780
Adv WP CLI
tarendai
0
760
WP CLI
tarendai
0
720
Other Decks in Technology
See All in Technology
AgentCore Managed Harness を使ってみよう
yakumo
2
210
今年注目する!データ分析プラットフォームでのAIの活用
nayuts
0
150
AI: Making Admin and Users, Lives Better
kbmsg
0
110
Do Ruby::Box dream of Modular Monolith?
joker1007
1
350
実践ハーネスエンジニアリング:TAKTで実現するAIエージェント制御 / Practical Harness Engineering: AI Agent Control Enabled by TAKT
nrslib
12
4.8k
ハーネスエンジニアリングの概要と設計思想
sergicalsix
9
5.3k
Rapid Start: Faster Internet Connections, with Ruby's Help
kazuho
2
760
Good Enough Types: Heuristic Type Inference for Ruby
riseshia
1
290
AWS Agent Registry の基礎・概要を理解する/aws-agent-registry-intro
ren8k
3
390
データを"持てない"環境でのアノテーション基盤設計
sansantech
PRO
1
140
[最強DB講義]推薦システム | 基礎編
recsyslab
PRO
1
180
LLM時代の検索アーキテクチャと技術的意思決定
shibuiwilliam
3
1.5k
Featured
See All Featured
Designing for humans not robots
tammielis
254
26k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
430
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.2k
ラッコキーワード サービス紹介資料
rakko
1
3.1M
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
1
530
Chasing Engaging Ingredients in Design
codingconduct
0
170
Designing for Timeless Needs
cassininazir
0
200
The Spectacular Lies of Maps
axbom
PRO
1
710
Information Architects: The Missing Link in Design Systems
soysaucechin
0
890
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
2.8k
GraphQLとの向き合い方2022年版
quramy
50
15k
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
370
Transcript
REST APIs for Absolute Beginners How to make a basic
endpoint, use it on the frontend, and convert existing code
What are my options?
Admin AJAX? add_action("wp_ajax_fruit", "fruit"); add_action("wp_ajax_nopriv_fruit", "fruit"); function fruit() { echo
"bananas"; exit; } jQuery.ajax({ type : "get", url : "https://example.com/wp-admin/admin-ajax.php", data : { action: "fruit" }, success: function(response) { // } });
A file in your theme/plugin you call?
REST API Endpoints
Where is the API? /wp-json
The rest API is a is a set of pages
that return JSON instead of HTML that have no state
REST actions GET - viewing POST - adding/updating DELETE -
deleting
Adding an Endpoint add_action( 'rest_api_init', function () { register_rest_route( 'tomjn/v1',
'/test/', array( 'methods' => 'GET', 'callback' => 'tomjn_rest_test' ) ); } ); function tomjn_rest_test() { return "moomins"; }
/wp-json/tomjn/v1/test
None
Viewing an endpoint jQuery.get( "https://tomjn.com/wp-json/tom/v1/test", function ( data ) {
console.log( data ); } );
How Do I Accept Parameters? function tomjn_rest_test( \WP_REST_Request $request )
{ return "moomins ".$request["val"]; } /wp-json/tomjn/v1/test?val=1 "moomins 1"
Converting AJAX calls • Register an endpoint • Use the
function WP Admin AJAX uses • Return what you want instead of printing it • Use the request parameter instead of $_GET $_POST etc • Update the URLs in your javascript
A Useful Helper wp_enqueue_script( 'wp-api' );
Creating Content $.ajax( { url: wpApiSettings.root + 'wp/v2/posts', method: 'POST',
beforeSend: function ( xhr ) { xhr.setRequestHeader( 'X-WP-Nonce', wpApiSettings.nonce ); }, data:{ 'title' : 'Hello Moon', 'content': 'Test post' } } ).done( function ( response ) { console.log( response ); } );
Updating Content $.ajax( { url: wpApiSettings.root + 'wp/v2/posts/1', method: 'POST',
beforeSend: function ( xhr ) { xhr.setRequestHeader( 'X-WP-Nonce', wpApiSettings.nonce ); }, data:{ 'title' : 'Updated Moon', 'content': 'even better post' } } ).done( function ( response ) { console.log( response ); } );
Deleting Content $.ajax( { url: wpApiSettings.root + 'wp/v2/posts/1', method: 'DELETE',
beforeSend: function ( xhr ) { xhr.setRequestHeader( 'X-WP-Nonce', wpApiSettings.nonce ); } } ).done( function ( response ) { console.log( response ); } );
Built in Endpoints Posts /wp/v2/posts Post Revisions /wp/v2/revisions Categories /wp/v2/categories
Tags /wp/v2/tags Pages /wp/v2/pages Comments /wp/v2/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
Useful Things
Adding REST support to CPT 'show_in_rest' => true,
WP.org REST API Handbook https://developer.wordpress.org/rest-api
Rest API Console
None
None
Questions? Tom J Nowell @tarendai https://tomjn.com Wordpress.com VIP* * we’re
hiring