Skip to content

Instantly share code, notes, and snippets.

@ishaadX
Created May 31, 2016 05:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ishaadX/69d4c5018433bd2f82b1d2c1f0033fa2 to your computer and use it in GitHub Desktop.
Save ishaadX/69d4c5018433bd2f82b1d2c1f0033fa2 to your computer and use it in GitHub Desktop.
`class Custom_Pages_Widgets extends WP_Widget{
public function __construct(){
$widget_ops = array(
'description' => __( "A list of your site’s Pages." )
);
parent::__construct( 'custom_widget_pages', __( 'Widget Pages' ), $widget_ops );
}
public function widget( $args, $instance ){
extract($args);
$select = empty( $instance['select'] ) ? '' : $instance['select'];
echo $args['before_widget'];
echo $args['before_title'];
?>
<div class="widget-list">
<ul>
<?php
foreach ($select as $post_id) {
echo '<li><a href="'.get_the_permalink($post_id).'">'.esc_attr(get_the_title($post_id)).'</a></li>';
}
?>
</ul>
</div>
<?php
echo $args['after_title'];
echo $args['after_widget'];
}
public function update( $new_instance, $old_instance ){
$instance = $old_instance;
$instance['select'] = esc_sql( $new_instance['select'] );
return $instance;
}
public function form( $instance ){
if( $instance )
$select = $instance['select'];
else
$select ='';
$get_page = get_pages( array(
'order' => 'DESC',
'posts_per_page' => 200,
'post_status' => 'publish',
'post_type' => 'page',
));
if( $get_page ){
printf(
'<select
multiple="multiple"
name="%s[]"
id="%s"
class="widefat"
size="10"
style="margin: 5px;">',
$this->get_field_name('select'),
$this->get_field_id('select')
);
foreach( $get_page as $page )
{
printf(
'<option value="%s" class="widefat" %s>%s</option>',
$page->ID,
in_array( $page->ID, $select) ? 'selected="selected"' : '',
$page->post_title
);
}
echo '</select>';
}
else
echo 'No Pages have found :(';
}
}
add_action('widgets_init', 'custom_widget_pages');
function custom_widget_pages(){
register_widget('Custom_Pages_Widgets');
}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment