Skip to content

Instantly share code, notes, and snippets.

@trepmal
Created June 5, 2012 22:59
Show Gist options
  • Select an option

  • Save trepmal/2878672 to your computer and use it in GitHub Desktop.

Select an option

Save trepmal/2878672 to your computer and use it in GitHub Desktop.
Include Page Widget
<?php
/*
Plugin Name: Include Page Widget
Plugin URI: http://trepmal.com/
Description: Widget. Display a page's content. That's it. Fork and adapt!
Version: 0.1
Author: Kailey Lampert
Author URI: http://kaileylampert.com
Copyright (C) 2012 Kailey Lampert
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
add_action( 'widgets_init', 'register_include_page_widget' );
function register_include_page_widget() {
register_widget( 'include_page_widget' );
}
class include_page_widget extends WP_Widget {
function __construct() {
$widget_ops = array('classname' => 'include-page-widget', 'description' => __( 'About this widget' ) );
$control_ops = array( 'width' => 300 );
parent::WP_Widget( 'includepagewidget', __( 'Include Page' ), $widget_ops, $control_ops );
}
function widget( $args, $instance ) {
extract( $args, EXTR_SKIP );
echo $before_widget;
switch( $instance['title_display'] ) {
case 'none':
$title = false;
break;
case 'page':
$title = get_the_title( $instance['page_id'] );
break;
case 'widget':
$title = $instance['title'];
break;
}
echo $title ? $before_title . $title . $after_title : '';
echo apply_filters( 'the_content', get_page( $instance['page_id'] )->post_content );
echo $after_widget;
} //end widget()
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = esc_attr( $new_instance['title'] );
$instance['title_display'] = esc_attr( $new_instance['title_display'] );
$instance['page_id'] = intval( $new_instance['page_id'] );
return $instance;
} //end update()
function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => 'Include Page Widget', 'title_display' => 'page', 'page_id' => 0 ) );
extract( $instance );
?>
<p style="width:63%;float:left;">
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' );?>
<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; ?>" />
</label>
</p>
<p style="width:33%;float:right;">
<label for="<?php echo $this->get_field_id('title_display'); ?>"><?php _e( 'Title Display:' );?></label>
<select id="<?php echo $this->get_field_id('title_display'); ?>" name="<?php echo $this->get_field_name('title_display'); ?>">
<?php $opts = array( 'none' => 'None', 'page' => 'Page title', 'widget' => 'Widget Title');
foreach ($opts as $k => $v ) {
echo "<option value='$k'". selected( $title_display, $k, false ) .">$v</option>";
}
?>
</select>
</p>
<p style="clear:both;">
<label for="<?php echo $this->get_field_id('page_id'); ?>"><?php _e( 'Page:' );?></label>
<?php
$args = array(
'depth' => 0,
'child_of' => 0,
'id' => $this->get_field_id('page_id'),
'selected' => $page_id,
'echo' => 1,
'name' => $this->get_field_name('page_id'),
);
wp_dropdown_pages( $args );
?>
</p>
<?php
} //end form()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment