Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress REST API
public function get_remote_posts() {
$posts = get_transient( 'remote_posts' );
if( empty( $posts ) ) {
$response = wp_remote_get( 'http://mysite.com/wp-json/wp/v2/posts/' );
if( is_wp_error( $response ) ) {
return array();
}
$posts = json_decode( wp_remote_retrieve_body( $response ) );
if( empty( $posts ) ) {
return array();
}
set_transient( 'remote_posts', $posts, HOUR_IN_SECONDS );
}
return $posts;
}
public function widget( $args, $instance ) {
$posts = $this->get_remote_posts();
if( empty( $posts ) ) {
return;
}
echo $args['before_widget'];
if( !empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $args['after_title'];
}
echo '<ul>';
foreach( $posts as $post ) {
echo '<li><a href="' . $post->link. '">' . $post->title->rendered . '</a></li>';
}
echo '</ul>';
echo $args['after_widget'];
}
$headers = array (
'Authorization' => 'Basic ' . base64_encode( 'mrawesome:awesomepass' ),
);
$response = wp_remote_request( 'http://mysite.com/wp-json/wp/v2/posts/1183/', array(
'method' => 'DELETE',
'headers' => $headers
));
$response = wp_remote_get( 'http://mysite.com/wp-json/wp/v2/posts/' );
/**
* Plugin Name: REST API Test Widget
* Plugin URI: http://danielpataki.com
* Description: This plugin adds a widget that pulls posts through the REST API
* Version: 1.0.0
* Author: Daniel Pataki
* Author URI: http://danielpataki.com
* License: GPL2
*/
class My_Author_List_Widget extends WP_Widget {
public function __construct() {
$widget_details = array(
'classname' => 'rest-api-test-widget',
'description' => 'A REST API test widget that pulls posts from a different website'
);
parent::__construct( 'rest-api-test-widget', 'REST API Test Widget', $widget_details );
}
public function form( $instance ) {
$title = ( !empty( $instance['title'] ) ) ? $instance['title'] : '';
?>
<p>
<label for="<?php echo $this->get_field_name( 'title' ); ?>">Title: </label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<?php
}
public function widget( $args, $instance ) {
echo $args['before_widget'];
if( !empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $args['after_title'];
}
// Main Widget Code Here
echo $args['after_widget'];
}
}
add_action( 'widgets_init', function(){
register_widget( 'My_Author_List_Widget' );
});
public function widget( $args, $instance ) {
$response = wp_remote_get( 'http://mysite.com/wp-json/wp/v2/posts/' );
if( is_wp_error( $response ) ) {
return;
}
$posts = json_decode( wp_remote_retrieve_body( $response ) );
if( empty( $posts ) ) {
return;
}
echo $args['before_widget'];
if( !empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $args['after_title'];
}
if( !empty( $posts ) ) {
echo '<ul>';
foreach( $posts as $post ) {
echo '<li><a href="' . $post->link. '">' . $post->title->rendered . '</a></li>';
}
echo '</ul>';
}
echo $args['after_widget'];
}
@newsapkota

This comment has been minimized.

Copy link

newsapkota commented Feb 4, 2018

Nice useful post.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.