'widget_section', 'description' => __('Display arbitrary text or HTML on certain sections of your site.')); $control_ops = array('width' => 400); $this->WP_Widget('section', __('Section'), $widget_ops, $control_ops); } function widget( $args, $instance ) { extract($args); // Most of the conditional tags will always return true when passed an empty array. So we need to pad them will some junk. $instance['special-pages'][] = -1; $instance['pages'][] = -1; $instance['categories'][] = -1; $instance['tags'][] = -1; $should_display = false; foreach($instance['special-pages'] as $key) { switch($key){ case "front": if(is_front_page()) $should_display = true; break; case "category": if(is_category($instance['categories'])) $should_display = true; break; case "tag": if(is_tag($instance['tags'])) $should_display = true; break; case "date": if(is_date()) $should_display = true; break; case "page": // Exclude the front page to avoid confusion if(!is_front_page() && is_page()) $should_display = true; break; case "post": if(is_single()) $should_display = true; break; case "comment": // Exclude the front page to avoid confusion if(!is_front_page() && is_singular() && comments_open()) $should_display = true; break; case "author": if(is_author()) $should_display = true; break; } if($should_display) break; } if(!$should_display) { if(is_page($instance['pages'])) $should_display = true; elseif(is_singular() && in_category($instance['categories'])) $should_display = true; elseif(is_singular() && has_tag($instance['tags'])) $should_display = true; } if($should_display) { echo $before_widget; ?>
'', 'display-title' => true, 'body' => '', 'special-pages' => array(), 'pages' => array(), 'categories' => array(), 'tags' => array()) ); $title = strip_tags($instance['title']); $display_title = (bool) $instance['display-title']; $special_pages = $instance['special-pages']; $pages = $instance['pages']; $categories = $instance['categories']; $tags = $instance['tags']; $body = format_to_edit($instance['body']); ?>

" />

$this->get_field_id('pages'), 'name' => $this->get_field_name('pages'), 'selected' => $pages)); ?>
$this->get_field_id('categories'), 'name' => $this->get_field_name('categories'), 'selected' => $categories)); ?>
$this->get_field_id('tags'), 'name' => $this->get_field_name('tags'), 'selected' => $tags)); ?>

line 714 as of 2.8) and the wp_category_checklist() function (in /wp-admin/includes/template.php -> line 490 as of 2.8) to make sense of the following code. */ function my_checklist($mode = 'pages', $args = '') { switch($mode) { case 'categories': $defaults = array( 'selected' => array(), 'echo' => 1, 'id' => 'page_id', 'name' => 'page_id' ); $data = get_categories('get=all'); $walker = new MyCategoryChecklistWalker; break; case 'tags': $defaults = array( 'selected' => array(), 'echo' => 1, 'id' => 'tag_id', 'name' => 'tag_id' ); $data = get_tags('get=all'); $walker = new MyTagChecklistWalker; break; case 'pages': default: $defaults = array( 'selected' => array(), 'echo' => 1, 'id' => 'category_id', 'name' => 'category_id' ); $data = get_pages('get=all'); $walker = new MyPageChecklistWalker; break; } $r = wp_parse_args( $args, $defaults ); extract( $r, EXTR_SKIP ); if ( ! empty($data) ) { $output = "\n"; } else { $output = "No $mode found."; } $output = apply_filters('wp_dropdown_pages', $output); if ( $echo ) echo $output; return $output; } class MyChecklistWalker extends Walker { function start_lvl(&$output, $depth, $args) { $indent = str_repeat("\t", $depth); $output .= "$indent\n"; } function end_el(&$output, $page, $depth, $args) { $output .= "\n"; } } class MyPageChecklistWalker extends MyChecklistWalker { var $tree_type = 'page'; var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID'); function start_el(&$output, $page, $depth, $args) { extract($args); $pad = str_repeat(' ', $depth * 4); $id = "{$id}-{$page->ID}"; $name .= '[]'; $title = esc_html($page->post_title); $output .= "
  • $padID\""; if ( in_array($page->ID, $selected) ) $output .= ' checked="checked"'; $output .= " />
    \n"; } } class MyCategoryChecklistWalker extends MyChecklistWalker { var $tree_type = 'category'; var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); function start_el(&$output, $category, $depth, $args) { extract($args); $pad = str_repeat(' ', $depth * 4); $id = "{$id}-{$category->term_id}"; $name .= '[]'; $title = esc_html($category->name); $output .= "
  • $padterm_id\""; if ( in_array($category->term_id, $selected) ) $output .= ' checked="checked"'; $output .= " />
    \n"; } } class MyTagChecklistWalker extends Walker { var $tree_type = 'tag'; var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); function start_el(&$output, $tag, $depth, $args) { extract($args); $id = "{$id}-{$tag->term_id}"; $name .= '[]'; $title = esc_html($tag->name); $output .= "
  • term_id\""; if ( in_array($tag->term_id, $selected) ) $output .= ' checked="checked"'; $output .= " />
    \n"; } } /* Finally, register this plugin/widget with wordpress. */ function widget_section_init() { if(is_admin()) { wp_enqueue_script('jquery-ui-tabs'); wp_enqueue_script('section-widget', plugins_url('section-widget/section-widget.js')); wp_enqueue_style('section-widget', plugins_url('section-widget/section-widget.css')); } register_widget('WP_Widget_Section'); } ### Function: Init Section Widget add_action('widgets_init', 'widget_section_init'); ?>