WordPress Meetup Kaunas 2021-01
WordPress Core Contributor, WordPress Kaunas Meetup co-organizer, WordCamp, WordSesh, TEDx speaker and one of the editors of the Lithuanian WordPress translation team.
Free, premium and custom WordPress plugin developer
Engineering Team Lead at
MVC
Event-driven
Visitor
Theme
Core
Plugins
Visitor
API
Core
Plugins
????
Visitor
API
Core
Plugins
Theme
Mobile App
3rd party client
...
Whitepaper "What to know before you go decoupled"
https://example.com/wp-json/
https://example.com/wp-json/wp/v2/posts
- list of latest posts
More details about using the API - in the handbook
Full list can be found calling the root endpoint or in the reference.
add_action( 'rest_api_init', 'arunas_endpoint' );
function arunas_endpoint () {
register_rest_route(
'myplugin/v1',
'/item/(?P<id>\d+)',
[
'methods' => 'GET',
'callback' => 'arunas_callback',
]
);
}
function arunas_callback( $request ) {
$item = $request->get_param( 'id' );
$data = method_to_get_data( $item );
$response = new WP_REST_Response( $data );
return $response;
}
https://example.com/grapql/
Make sure pretty permalinks are enabled and updated
add_action( 'graphql_register_types', 'register_dog_type' );
function register_dog_type() {
register_graphql_object_type( 'Dog', [
'description' => __( "Man's best friend", 'your-textdomain' ),
'fields' => [
'name' => [
'type' => 'String',
'description' => __( 'The name of the dog', 'your-textdomain' ),
],
'breed' => [
'type' => 'String',
'description' => __( 'The Breed of the dog', 'your-textdomain' ),
],
'age' => [
'type' => 'Integer',
'description' => __( 'The age, in years, of the dog', 'your-textdomain' ),
],
],
] );
}
add_action( 'graphql_register_types', 'register_dog_field' );
function register_dog_field() {
register_graphql_field( 'RootQuery', 'getDog', [
'description' => __( 'Get a dog', 'your-textdomain' ),
'type' => 'Dog',
'resolve' => function() {
// Here you need to return data that matches the shape of the "Dog" type.
// You could get the data from the WP Database, an external API, or
// static values. For example sake, we will just return a hard-coded array.
return [
'name' => 'Sparky',
'breed' => 'Golden Retriever',
'age' => 8
];
}
] );
}