Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Created September 7, 2017 18:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/d77119838c89f3247d35009e73bed43b to your computer and use it in GitHub Desktop.
Save wpmudev-sls/d77119838c89f3247d35009e73bed43b to your computer and use it in GitHub Desktop.
WordPress - Simple Rest API example
<?php
/*
Plugin Name: REST Api Example
Plugin URI: https://premium.wpmudev.org/
Description: A usage example of the wp REST API
Author: Panos Lyrakis @ WPMUDEV
Author URI: https://premium.wpmudev.org/
License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WPMUDEV_RESTAPI_Example' ) ) {
class WPMUDEV_RESTAPI_Example {
private static $_instance = null;
private static $_api_url = 'http://localhost/wpmudev2/wp-json/wp/v2/';
public static function get_instance() {
if( is_null( self::$_instance ) ){
self::$_instance = new WPMUDEV_RESTAPI_Example();
}
return self::$_instance;
}
public function __construct() {
add_action('init', array( $this, 'register_cpt' ) );
add_shortcode( 'wpmudev_example_rest_call', array( $this, 'call_rest_sc' ) );
}
public function call_rest_sc( $atts ){
$atts = shortcode_atts(
array(
'player_id' => '',
), $atts, 'WPMUDEV_RESTAPI_Example/call_rest_sc' );
$wp_request_url = self::$_api_url . 'player/' . $atts[ 'player_id' ];
$wp_request_post_response = wp_remote_request(
$wp_request_url,
array(
'method' => 'GET'
)
);
$response_code = wp_remote_retrieve_response_code( $wp_request_post_response ) . ' ' . wp_remote_retrieve_response_message( $wp_request_post_response );
$response = $wp_request_post_response;
print_r( $response );
}
public function register_cpt(){
$labels = array(
'name' => _x( 'Players', 'post type general name', 'some-textdomain' ),
'singular_name' => _x( 'Player', 'post type singular name', 'some-textdomain' ),
'menu_name' => _x( 'Players', 'admin menu', 'some-textdomain' ),
'name_admin_bar' => _x( 'Player', 'add new on admin bar', 'some-textdomain' ),
'add_new' => _x( 'Add New', 'book', 'some-textdomain' ),
'add_new_item' => __( 'Add New Player', 'some-textdomain' ),
'new_item' => __( 'New Player', 'some-textdomain' ),
'edit_item' => __( 'Edit Player', 'some-textdomain' ),
'view_item' => __( 'View Player', 'some-textdomain' ),
'all_items' => __( 'All Players', 'some-textdomain' ),
'search_items' => __( 'Search Players', 'some-textdomain' ),
'parent_item_colon' => __( 'Parent Players:', 'some-textdomain' ),
'not_found' => __( 'No Players found.', 'some-textdomain' ),
'not_found_in_trash' => __( 'No Players found in Trash.', 'some-textdomain' )
);
$args = array(
'labels' => $labels,
'description' => __( 'Description.', 'some-textdomain' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'player' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'show_in_rest' => true,
'rest_base' => 'player',
'rest_controller_class' => 'WP_REST_Posts_Controller',
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
);
register_post_type( 'player', $args );
}
}
add_action( 'plugins_loaded', function(){
$GLOBALS['WPMUDEV_RESTAPI_Example'] = WPMUDEV_RESTAPI_Example::get_instance();
}, 10 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment