Created
December 2, 2014 16:15
-
-
Save hearvox/d01185ee8bc5ccffd692 to your computer and use it in GitHub Desktop.
WordPress Pages Dropdown Widget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Pages dropdown widget" */ | |
// Based on WP_Widget_Pages class in /wp-includes/default-widgets.php | |
class Hearvox_Widget_Pages_Dropdown extends WP_Widget { | |
/** | |
* Register widget with WordPress. | |
*/ | |
function __construct() { | |
parent::__construct( | |
'hearvox_pages_dropdown', // Base ID | |
__( 'Pages Dropdown', 'text_domain' ), // Name | |
array( 'description' => __( 'Your site’s Pages in a dropdown menu with a nav button.', 'text_domain' ), ) // Args | |
); | |
} | |
/** | |
* Front-end display of widget. | |
* | |
* @see WP_Widget::widget() | |
* | |
* @param array $args Widget arguments. | |
* @param array $instance Saved values from database. | |
*/ | |
function widget( $args, $instance ) { | |
extract( $args ); | |
$title = apply_filters('widget_title', empty( $instance['title'] ) ? __( 'Pages Dropdown' ) : $instance['title'], $instance, $this->id_base); | |
$sortby = empty( $instance['sortby'] ) ? 'menu_order' : $instance['sortby']; | |
$exclude = empty( $instance['exclude'] ) ? '' : $instance['exclude']; | |
if ( $sortby == 'menu_order' ) | |
$sortby = 'menu_order, post_title'; | |
$out = '<form action="' . get_bloginfo( 'url' ) . '" method="get">' . "\n"; | |
$out .= wp_dropdown_pages( array( 'echo' => false, 'sort_column' => $sortby, 'exclude' => $exclude ) ); | |
$out .= "\n" . '<br /><input type="submit" name="submit" value="Go to Page" />' . "\n" . '</form>'; | |
if ( !empty( $out ) ) { | |
echo $before_widget; | |
if ( $title) | |
echo $before_title . $title . $after_title; | |
?> | |
<ul> | |
<?php echo $out; ?> | |
</ul> | |
<?php | |
echo $after_widget; | |
} | |
} | |
/** | |
* Sanitize widget form values as they are saved. | |
* | |
* @see WP_Widget::update() | |
* | |
* @param array $new_instance Values just sent to be saved. | |
* @param array $old_instance Previously saved values from database. | |
* | |
* @return array Updated safe values to be saved. | |
*/ | |
function update( $new_instance, $old_instance ) { | |
$instance = $old_instance; | |
$instance['title'] = strip_tags( $new_instance['title'] ); | |
if ( in_array( $new_instance['sortby'], array( 'post_title', 'menu_order', 'ID' ) ) ) { | |
$instance['sortby'] = $new_instance['sortby']; | |
} else { | |
$instance['sortby'] = 'menu_order'; | |
} | |
$instance['exclude'] = strip_tags( $new_instance['exclude'] ); | |
return $instance; | |
} | |
/** | |
* Back-end widget form. | |
* | |
* @see WP_Widget::form() | |
* | |
* @param array $instance Previously saved values from database. | |
*/ | |
function form( $instance ) { | |
//Defaults | |
$instance = wp_parse_args( (array) $instance, array( 'sortby' => 'post_title', 'title' => '', 'exclude' => '') ); | |
$title = esc_attr( $instance['title'] ); | |
$exclude = esc_attr( $instance['exclude'] ); | |
?> | |
<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 $title; ?>" /></p> | |
<p> | |
<label for="<?php echo $this->get_field_id('sortby'); ?>"><?php _e( 'Sort by:' ); ?></label> | |
<select name="<?php echo $this->get_field_name('sortby'); ?>" id="<?php echo $this->get_field_id('sortby'); ?>" class="widefat"> | |
<option value="post_title"<?php selected( $instance['sortby'], 'post_title' ); ?>><?php _e('Page title'); ?></option> | |
<option value="menu_order"<?php selected( $instance['sortby'], 'menu_order' ); ?>><?php _e('Page order'); ?></option> | |
<option value="ID"<?php selected( $instance['sortby'], 'ID' ); ?>><?php _e( 'Page ID' ); ?></option> | |
</select> | |
</p> | |
<p> | |
<label for="<?php echo $this->get_field_id('exclude'); ?>"><?php _e( 'Exclude:' ); ?></label> <input type="text" value="<?php echo $exclude; ?>" name="<?php echo $this->get_field_name('exclude'); ?>" id="<?php echo $this->get_field_id('exclude'); ?>" class="widefat" /> | |
<br /> | |
<small><?php _e( 'Page IDs, separated by commas.' ); ?></small> | |
</p> | |
<?php | |
} | |
} | |
// Register and load the widget | |
function hearvox_load_widget() { | |
register_widget( 'Hearvox_Widget_Pages_Dropdown' ); | |
} | |
add_action( 'widgets_init', 'hearvox_load_widget' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment