Skip to content

Instantly share code, notes, and snippets.

@igorbenic
Created May 4, 2023 01:20
Show Gist options
  • Save igorbenic/f1e4cb5c3411f982509abecb53127acd to your computer and use it in GitHub Desktop.
Save igorbenic/f1e4cb5c3411f982509abecb53127acd to your computer and use it in GitHub Desktop.
Developer Challenge: Create a Custom WordPress Widget | subscribe for more at https://www.ibenic.com/newsletter/
<?php
/**
* Plugin Name: Recent Widget Challenge from ibenic.com/newsletter
*/
/*
You will create a custom WordPress widget that displays a list of the most recent posts.
Each recent post should have:
- Title
- Thumbnail
- Excerpt
The widget should have a title that can be customized by the user.
Extra Credit:
- Show the number of comments for each post.
- In the admin area, add a "Number of posts" field. If it's set to 3, it will show 3 recent posts.
*/
class Recent_Posts_Widget extends WP_Widget {
function __construct() {
parent::__construct(
// Base ID of your widget
'recent_posts_widget',
// Widget name will appear in UI
__('Recent Posts Widget', 'recent_posts_widget_domain'),
// Widget description
array( 'description' => __( 'A custom widget that displays a list of the most recent posts with thumbnails', 'recent_posts_widget_domain' ), )
);
}
// Creating widget front-end
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
// before and after widget arguments are defined by themes
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];
// This is where you run the code and display the output
$this->get_recent_posts( $instance );
echo $args['after_widget'];
}
// Widget Backend
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
} else {
$title = __( 'New title', 'recent_posts_widget_domain' );
}
if ( isset( $instance[ 'number' ] ) ) {
$number = $instance[ 'number' ];
} else {
$number = 5;
}
// Widget admin form
?>
<p>
<label for="<?php echo $this->get_field_id( '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>
<p>
<label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="number" value="<?php echo esc_attr( $number ); ?>" />
</p>
<?php
}
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
$instance['number'] = ( ! empty( $new_instance['number'] ) ) ? absint( $new_instance['number'] ) : 5;
return $instance;
}
// Get the most recent posts with thumbnails
private function get_recent_posts( $args ) {
$args = array(
'posts_per_page' => isset( $args['number'] ) ? absint( $args['number'] ) : 5,
'post_type' => 'post',
'orderby' => 'date',
'order' => 'DESC'
);
$posts = get_posts( $args );
if ( $posts ) {
echo '<ul>';
foreach ( $posts as $post ) {
$number = get_comments_number( $post );
echo '<li><a href="' . get_permalink( $post ) . '">' . get_the_post_thumbnail( $post ) . get_the_title( $post ) . ' (' . $number . ')</a></li>';
}
echo '</ul>';
}
}
}
// Register and load the widget
function recent_posts_widget_load() {
register_widget( 'Recent_Posts_Widget' );
}
add_action( 'widgets_init', 'recent_posts_widget_load');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment