About Rest API @ WC Udaipur

Lets Talk about WP Rest API

Everything

SLIDE PDF

Download PDF

Rest Theme

It’s a WordPress Theme Made Using Vue & WP Rest Api By Gilbert.

https://github.com/gilbitron/wp-rest-theme


Snippets

Just passing the gist for Simple way of adding rest endpoint.

These endpoints are targeting wp-json/myapi/v1/cars/ route.

Register GET Endpoint :-


<?php
/**
* This is our callback function that embeds our phrase in a WP_REST_Response
*/
function car_get_callback() {
$cars = array("Honda","Suzuki","Hundai");
// rest_ensure_response() wraps the data we want to return into a WP_REST_Response, and ensures it will be properly returned.
return rest_ensure_response( $cars );
}
/**
* This function is where we register our routes for our example endpoint.
*/
function register_car_route() {
// register_rest_route() handles more arguments but we are going to stick to the basics for now.
register_rest_route( 'myapi/v1', '/cars', array(
// By using this constant we ensure that when the WP_REST_Server changes our readable endpoints will work as intended.
'methods' => WP_REST_Server::READABLE, // This tells make it available to GET Methods.
// Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class.
'callback' => 'car_get_callback',
) );
}
add_action( 'rest_api_init', 'register_car_route' );

view raw

getexample.php

hosted with ❤ by GitHub

Register POST Endpoint –


<?php
/**
* This is our callback function that embeds our phrase in a WP_REST_Response
*/
function car_post_callback() {
// Do some code here…
// rest_ensure_response() wraps the data we want to return into a WP_REST_Response, and ensures it will be properly returned.
return rest_ensure_response( array("updated"=>true) );
}
/**
* This function is where we register our routes for our example endpoint.
*/
function register_car_route() {
register_rest_route( 'myapi/v1', '/cars', array(
// By using this constant we ensure that when the WP_REST_Server changes our editable endpoints will work as intended.
'methods' => WP_REST_Server::EDITABLE, // This tells make it available to POST Methods.
// Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class.
'callback' => 'car_post_callback',
) );
}
add_action( 'rest_api_init', 'register_car_route' );

view raw

postexample.ph

hosted with ❤ by GitHub

Register Delete Endpoint :- 


<?php
/**
* This is our callback function that embeds our phrase in a WP_REST_Response
*/
function car_delete_callback() {
// Do some code here…
// rest_ensure_response() wraps the data we want to return into a WP_REST_Response, and ensures it will be properly returned.
return rest_ensure_response( array("deleted"=>true) );
}
/**
* This function is where we register our routes for our example endpoint.
*/
function register_car_route() {
register_rest_route( 'myapi/v1', '/cars', array(
// By using this constant we ensure that when the WP_REST_Server changes our deleteable endpoints will work as intended.
'methods' => WP_REST_Server::DELETABLE, // This tells make it available to DELETE Methods.
// Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class.
'callback' => 'car_delete_callback',
) );
}
add_action( 'rest_api_init', 'register_car_route' );

Register CPT Type :- 


<?php
/**
* Register a book post type, with REST API support
*
* Based on example at: https://codex.wordpress.org/Function_Reference/register_post_type
*/
add_action( 'init', 'my_book_cpt' );
function my_book_cpt() {
$labels = array(
'name' => _x( 'Books', 'post type general name', 'your-plugin-textdomain' ),
'singular_name' => _x( 'Book', 'post type singular name', 'your-plugin-textdomain' ),
'menu_name' => _x( 'Books', 'admin menu', 'your-plugin-textdomain' ),
'name_admin_bar' => _x( 'Book', 'add new on admin bar', 'your-plugin-textdomain' ),
'add_new' => _x( 'Add New', 'book', 'your-plugin-textdomain' ),
'add_new_item' => __( 'Add New Book', 'your-plugin-textdomain' ),
'new_item' => __( 'New Book', 'your-plugin-textdomain' ),
'edit_item' => __( 'Edit Book', 'your-plugin-textdomain' ),
'view_item' => __( 'View Book', 'your-plugin-textdomain' ),
'all_items' => __( 'All Books', 'your-plugin-textdomain' ),
'search_items' => __( 'Search Books', 'your-plugin-textdomain' ),
'parent_item_colon' => __( 'Parent Books:', 'your-plugin-textdomain' ),
'not_found' => __( 'No books found.', 'your-plugin-textdomain' ),
'not_found_in_trash' => __( 'No books found in Trash.', 'your-plugin-textdomain' )
);
$args = array(
'labels' => $labels,
'description' => __( 'Description.', 'your-plugin-textdomain' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'book' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'show_in_rest' => true,
'rest_base' => 'books',
'rest_controller_class' => 'WP_REST_Posts_Controller',
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
);
register_post_type( 'book', $args );
}

Rest API support on existing post type


<?php
/**
* Add REST API support to an already registered post type.
*/
add_action( 'init', 'my_custom_post_type_rest_support', 25 );
function my_custom_post_type_rest_support() {
global $wp_post_types;
//be sure to set this to the name of your post type!
$post_type_name = 'planet';
if( isset( $wp_post_types[ $post_type_name ] ) ) {
$wp_post_types[$post_type_name]->show_in_rest = true;
// Optionally customize the rest_base or controller class
$wp_post_types[$post_type_name]->rest_base = $post_type_name;
$wp_post_types[$post_type_name]->rest_controller_class = 'WP_REST_Posts_Controller';
}
}

Extend Existing WordPress Endpoint


<?php
add_action( 'rest_api_init', 'register_the_ranking_field' );
function register_the_ranking_field() {
register_rest_field( 'post',
'post_ranking',
array(
'get_callback' => 'get_the_post_ranking',
'update_callback' => 'update_the_post_ranking',
'schema' => null,
)
);
}
function get_the_post_ranking( $object, $field_name, $request ) {
$post_id = $object[ 'id' ];
$value = get_post_meta($post_id,"_post_ranking", true );
if(!$value)
$value = "1";
return $value;
}
function update_the_post_ranking($value, $object, $field_name) {
// do some security role checks
if ( ! $value || ! is_string( $value ) ) {
return;
}
return update_post_meta( $object->ID, "_post_ranking", strip_tags( $value ) );
}