Skip to content

Instantly share code, notes, and snippets.

@keesiemeijer
Last active September 13, 2017 14:57
Show Gist options
  • Save keesiemeijer/ef760592e2f7e039155ea0dfec3d3c6d to your computer and use it in GitHub Desktop.
Save keesiemeijer/ef760592e2f7e039155ea0dfec3d3c6d to your computer and use it in GitHub Desktop.
Widget with checkbox
<?php
add_action( 'widgets_init', 'register_my_scheduled_posts_widget' );
function register_my_scheduled_posts_widget() {
register_widget( 'My_Scheduled_Posts_Widget' );
}
class My_Scheduled_Posts_Widget extends WP_Widget {
public function __construct() {
$widget_ops = array(
'classname' => 'scheduled_recent_entries',
'description' => __( 'Your site&#8217;s most recent (scheduled) Posts.' ),
'customize_selective_refresh' => true,
);
parent::__construct( 'scheduled-recent-posts', __( 'Scheduled Posts' ), $widget_ops );
}
/**
* Widget display.
*/
public function widget( $args, $instance ) {
$status_future = (bool) $instance['status_future'];
/* ***** Query for recent posts and display them in the front end here ***** */
}
/**
* Update widget settings.
*/
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['status_future'] = isset( $new_instance['status_future'] ) ? (bool) $new_instance['status_future'] : false;
return $instance;
}
/**
* Widget settings form.
*/
public function form( $instance ) {
$status_future = isset ( $instance['status_future'] ) ? (bool) $instance['status_future'] : false;
?>
<p>
<input class="checkbox" type="checkbox" <?php checked( $status_future, true ); ?> id="<?php echo $this->get_field_id( 'status_future' ); ?>" name="<?php echo $this->get_field_name( 'status_future' ); ?>">
<label for="<?php echo $this->get_field_id( 'status_future' ); ?>"><?php _e( 'Show posts with future dates only?', 'custom-post-type-date-archives' ); ?></label>
</p>
<?php
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment