Skip to content

Instantly share code, notes, and snippets.

@keesiemeijer
Last active September 13, 2017 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keesiemeijer/d5a80db6930a38f9abb3045af270bb70 to your computer and use it in GitHub Desktop.
Save keesiemeijer/d5a80db6930a38f9abb3045af270bb70 to your computer and use it in GitHub Desktop.
Wiget with dropdown and back compatibility
<?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 );
}
/**
* Get validated widget instance settings with back compatibility.
*/
function get_instance_settings( $instance ) {
/*
* Check if the old checkbox 'status_future' setting exists.
* And if the new dropdown 'include_posts' setting doesn't exist.
*/
if ( isset( $instance['status_future'] ) && ! isset( $instance['include_posts'] ) ) {
// Back compatibility!
// Update the dropdown 'include_posts' setting with the old checkbox 'status_future' setting if it was set.
$instance['include_posts'] = $instance['status_future'] ? 'future' : 'all';
}
// Check if the dropdown 'include_posts' setting exists and has a value.
if ( ! ( isset( $instance['include_posts'] ) && $instance['include_posts'] ) ) {
// Set it to default 'all'
$instance['include_posts'] = 'all';
}
return $instance;
}
/**
* Widget display.
*/
public function widget( $args, $instance ) {
// Get validated instance settings with back compatibility.
$instance = $this->get_instance_settings( $instance );
$include = (string) $instance['include_posts'];
/* ***** Query for posts and display them here ***** */
}
/**
* Update widget settings.
*/
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['include_posts'] = $new_instance['include_posts'];
// Back compatibility!
// Don't update (checkbox) 'status_future' setting ever again.
unset( $instance['status_future'] );
return $instance;
}
/**
* Widget settings form.
*/
public function form( $instance ) {
// Get validated instance settings with back compatibility.
$instance = $this->get_instance_settings( $instance );
$include = $instance['include_posts'];
// Dropdown options
$include_options = array(
'all' => __( 'all posts', 'your-plugin-textdomain' ),
'future' => __( 'posts with future dates only', 'your-plugin-textdomain' ),
'year' => __( 'posts from the current year', 'your-plugin-textdomain' ),
'month' => __( 'posts from the current month', 'your-plugin-textdomain' ),
'day' => __( 'posts from today', 'your-plugin-textdomain' ),
);
?>
<p>
<label for="<?php echo $this->get_field_id( 'include' ); ?>">
<?php _e( 'Include Posts:', 'your-plugin-textdomain' ); ?>
</label>
<select name="<?php echo $this->get_field_name( 'include' ); ?>" id="<?php echo $this->get_field_id( 'include' ); ?>" class="widefat">
<?php foreach ( $include_options as $name => $label ) : ?>
<option value="<?php echo esc_attr( $name ); ?>" <?php selected( $include, $name ); ?>>
<?php echo $label; ?>
</option>
<?php endforeach; ?>
</select>
</p>
<?php
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment