Skip to content

Instantly share code, notes, and snippets.

@weszty
Forked from wpmu-authors/widget.php
Created January 15, 2023 22:54
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 weszty/78f1accef47464b46671c5e833292fef to your computer and use it in GitHub Desktop.
Save weszty/78f1accef47464b46671c5e833292fef to your computer and use it in GitHub Desktop.
widget.php
<?php
class My_Favorite_Director_Widget extends WP_Widget
{
function __construct()
{
$widget_details = array(
'classname' => 'my-favorite-director-widget',
'description' => 'Display movies from your favorite director.'
);
parent::__construct( 'my-favorite-director', 'My Favorite Director', $widget_details );
}
function form( $instance ) {
$title = ( empty( $instance['title'] ) ) ? '' : $instance['title'];
?>
<p>
<label for="<?php echo $this->get_field_name( 'title' ); ?>"><?php _e( '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
}
function update( $new_instance, $old_instance ) {
return $new_instance;
}
function widget( $args, $instance ) {
$response = wp_remote_get( 'http://netflixroulette.net/api/api.php?director=Martin%20Scorsese', $args );
$data = json_decode( wp_remote_retrieve_body( $response ), true );
if( empty( $data ) ) {
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( $data as $movie ) {
echo "<li>" . $movie['show_title'] . "</li>";
}
echo "</ul>";
echo $args['after_widget'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment