Created
February 18, 2012 09:43
-
-
Save jelmervdl/1858500 to your computer and use it in GitHub Desktop.
Contents of a Page widget for Wordpress
This file contains hidden or 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
<?php | |
/* | |
Plugin Name: Page widget | |
Description: Use a page as a widget in the sidebar | |
Author: Jelmer van der Linde | |
Version: 1.0 | |
Author URI: http://ikhoefgeen.nl | |
*/ | |
add_action('widgets_init', array('IHG_PageWidget', 'register_widget')); | |
class IHG_PageWidget extends WP_Widget | |
{ | |
public function __construct() | |
{ | |
parent::__construct( false, 'Contents of a page'); | |
} | |
public function widget($args, $instance) | |
{ | |
if (!($page = $this->get_the_page($instance))) | |
return; | |
printf($args['before_widget'], $args['widget_id'], $args['widget_name']); | |
echo apply_filters('the_content', $page->post_content); | |
echo $args['after_widget']; | |
} | |
public function update($new_instance, $old_instance) | |
{ | |
$instance = is_array($old_instance) | |
? $old_instance | |
: array(); | |
$instance['page_id'] = (int) $new_instance['page_id']; | |
return $instance; | |
} | |
public function form($instance) | |
{ | |
echo '<p>'; | |
echo '<label for="' . $this->get_field_id('page_id') . '">' . __('Page') . ':</label>'; | |
wp_dropdown_pages(array( | |
'selected' => isset($instance['page_id']) ? $instance['page_id'] : 0, | |
'name' => $this->get_field_name('page_id') | |
)); | |
echo '</p>'; | |
} | |
private function get_the_page($instance) | |
{ | |
return !empty($instance['page_id']) | |
? get_page($instance['page_id']) | |
: null; | |
} | |
static public function register_widget() | |
{ | |
register_widget(__CLASS__); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment