+ All Categories
Home > Technology > Practical REST API

Practical REST API

Date post: 15-Apr-2017
Category:
Upload: aki-bjoerklund
View: 289 times
Download: 1 times
Share this document with a friend
32
Practical REST API Why admin-ajax.php should be avoided and how Aki Björklund CTO,H1 @akibjorklund
Transcript
Page 1: Practical REST API

Practical REST APIWhy admin-ajax.php should be avoided and how

Aki Björklund CTO,H1

@akibjorklund

Page 2: Practical REST API

Communication between JavaScript and PHP is done with Ajax

Page 3: Practical REST API

Historically that is done in WordPress by calling

/wp-admin/admin-ajax.php

Page 4: Practical REST API

Why admin-ajax.php is bad?

Page 5: Practical REST API

1. Inefficient

Page 6: Practical REST API

2. Not cached

Page 7: Practical REST API

3. No infrastructure like authentication, data

validation, …

Page 8: Practical REST API

4.Ad hoc: messy

Page 9: Practical REST API

WordPress REST API in core from 4.4

Page 10: Practical REST API

There are no built-in endpoints yet

Page 11: Practical REST API

Unless you install the REST API plugin

Page 12: Practical REST API

In many cases no need to write any PHP to do a lot.

Page 13: Practical REST API

But what can you do just with what the core already has?

Page 14: Practical REST API

Example: Comment Reactions

Page 15: Practical REST API
Page 16: Practical REST API

Converting admin-ajax.php to REST API

Page 17: Practical REST API

Before

Page 18: Practical REST API
Page 19: Practical REST API
Page 20: Practical REST API
Page 21: Practical REST API
Page 22: Practical REST API

After

Page 23: Practical REST API
Page 24: Practical REST API
Page 25: Practical REST API
Page 26: Practical REST API
Page 27: Practical REST API

Best practice is to implement what you just saw as a REST Controller. Currently available only with the REST API plugin.

Page 28: Practical REST API

Summary

Page 29: Practical REST API

Use the REST API instead of admin-ajax.php

Page 30: Practical REST API

It’s faster and cleaner

Page 31: Practical REST API

It’s also both easy to implement and migrate to

Page 32: Practical REST API

.

Aki Björklund CTO,H1

@akibjorklund


Recommended